Search code examples
javascriptwebpackmodulegulp

Gulp webpack can't resolve imported class


I'm using gulp with babel-loader and webpack. My gulp is throwing an error that says gulp can't find the separated file I'm importing

This is the error (followed by others due to the class missing)

internal/streams/legacy.js:59
      throw er; // Unhandled stream error in pipe.
      ^
Error: ./src/client.js
Module not found: Error: Can't resolve 'game/modules/Game' in '/home/k3nzie/projects/jsGame/src'
resolve 'game/modules/Game' in '/home/k3nzie/projects/jsGame/src'
  Parsed request is a module
  using description file: /home/k3nzie/projects/jsGame/package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /home/k3nzie/projects/jsGame/package.json (relative path: ./src)
    resolve as module
      /home/k3nzie/projects/jsGame/src/node_modules doesn't exist or is not a directory
      /home/k3nzie/projects/node_modules doesn't exist or is not a directory
      /home/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /home/k3nzie/projects/jsGame/node_modules
        using description file: /home/k3nzie/projects/jsGame/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
        after using description file: /home/k3nzie/projects/jsGame/package.json (relative path: ./node_modules)
          using description file: /home/k3nzie/projects/jsGame/package.json (relative path: ./node_modules/game/modules/Game)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /home/k3nzie/projects/jsGame/node_modules/game/modules/Game doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/k3nzie/projects/jsGame/node_modules/game/modules/Game.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/k3nzie/projects/jsGame/node_modules/game/modules/Game.json doesn't exist
            as directory
              /home/k3nzie/projects/jsGame/node_modules/game/modules/Game doesn't exist
      looking for modules in /home/k3nzie/node_modules
        No description file found
        Field 'browser' doesn't contain a valid alias configuration
        No description file found
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          /home/k3nzie/node_modules/game/modules/Game doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          /home/k3nzie/node_modules/game/modules/Game.js doesn't exist
        .json
          Field 'browser' doesn't contain a valid alias configuration
          /home/k3nzie/node_modules/game/modules/Game.json doesn't exist
        as directory
          /home/k3nzie/node_modules/game/modules/Game doesn't exist
[/home/k3nzie/projects/jsGame/src/node_modules]
[/home/k3nzie/projects/node_modules]
[/home/node_modules]
[/node_modules]
[/home/k3nzie/node_modules/package.json]
[/home/k3nzie/projects/jsGame/node_modules/game/modules/Game]
[/home/k3nzie/projects/jsGame/node_modules/game/modules/Game.js]
[/home/k3nzie/node_modules/game/modules/Game/package.json]
[/home/k3nzie/projects/jsGame/node_modules/game/modules/Game.json]
[/home/k3nzie/node_modules/game/modules/Game]
[/home/k3nzie/projects/jsGame/node_modules/game/modules/Game]
[/home/k3nzie/node_modules/game/modules/Game.js]
[/home/k3nzie/node_modules/game/modules/Game.json]
[/home/k3nzie/node_modules/game/modules/Game]
 @ ./src/client.js 1:0-41main.js from UglifyJs
Unexpected token: name (socket) [main.js:74,4]

While this is the files structure

- src
 - game
  - modules/
   -  Game.js (class)
- client.js (main file)

I've imported the Game class inside client.js like this:

import Game from 'game/modules/Game';

It should find the class but it doesn't, what am I doing wrong?

edit: errors after corrections

throw er; // Unhandled stream error in pipe.
      ^
Error: main.js from UglifyJs
Unexpected token: name (game) [main.js:82,4]

if I comment out another variable

internal/streams/legacy.js:59
      throw er; // Unhandled stream error in pipe.
      ^
Error: main.js from UglifyJs
Unexpected token: name (socket) [main.js:75,4]

these are the variables inside client.js after correct class Game import:

let socket = io();
let game = new Game();

Solution

  • Import path is wrong, try with import Game from './game/modules/Game';