【问题标题】:Gulp webpack can't resolve imported classGulp webpack 无法解析导入的类
【发布时间】:2018-02-08 17:13:51
【问题描述】:

我将 gulp 与 babel-loader 和 webpack 一起使用。我的 gulp 抛出一个错误,说 gulp 找不到我要导入的分隔文件

这是错误(由于缺少类而被其他人跟踪)

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]

虽然这是文件结构

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

我在client.js 中导入了Game 类,如下所示:

import Game from 'game/modules/Game';

它应该找到类,但它没有,我做错了什么?

编辑:更正后的错误

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

如果我注释掉另一个变量

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]

这些是正确的类游戏导入后 client.js 中的变量:

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

【问题讨论】:

    标签: javascript webpack module gulp


    【解决方案1】:

    导入路径错误,用import Game from './game/modules/Game'试试;

    【讨论】:

    • 解决了,但是在声明变量(以及包含类的变量)时仍然会抛出错误,我编辑了帖子,你能检查一下吗?这很奇怪......
    • UglifyJS的问题,试试这个解决方案:stackoverflow.com/questions/46901443/…
    猜你喜欢
    • 2018-01-25
    • 2015-09-05
    • 2018-03-14
    • 2020-02-21
    • 2018-08-05
    • 2021-03-25
    • 1970-01-01
    • 2020-09-13
    • 2012-11-22
    相关资源
    最近更新 更多