无法解决" app.js" - Webpack

时间:2017-04-16 06:03:27

标签: javascript webpack

问题

所以,我正在使用Webpack和ES6开发Web应用程序。当我尝试运行Webpack时,它告诉我它无法解决" app.js"。我在整个互联网上寻找解决方案,但我找不到一个,有人可以帮助我吗? 完整错误是:

ERROR in ./assets/js/main.js
Module not found: Error: Can't resolve 'app.js' in 'C:\Users\sidna\Dropbox\Dev Stuff\Web Apps\Mondrian Generator\assets\js'
 @ ./assets/js/main.js 4:0-17

webpack.config.js

module.exports = {
entry: './assets/js/main.js',
output: {
    filename: 'assets/js/build.js',
},
watch: true,
module: {
    loaders: [
        {
            test: /\.scss$/, loader: "style-loader!css-loader!sass-loader"
        },
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['env']
            }
          }
        }
    ],
}
};

app.js(空)

main.js

// SCRIPTS
require("app.js");

// STYLES
require("../css/large.scss");

的package.json

{
"name": "mondrian-generator",
  "version": "1.0.0",
  "description": "Create your own Mondrian",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "license": "ISC",
  "dependencies": {
    "css-loader": "^0.28.0",
    "style-loader": "^0.16.1",
    "webpack": "^2.4.1"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^6.4.1",
    "babel-preset-env": "^1.4.0",
    "node-sass": "^4.5.2",
    "sass-loader": "^6.0.3",
    "script-loader": "^0.7.0",
    "webpack": "^2.4.1"
  }
}

1 个答案:

答案 0 :(得分:1)

当导入既不是绝对路径(从/开始)也不明确是相对路径(从./../开始)时,它被解析为模块,这意味着它是Loading from node_modules Folders

您的app.js不在node_modules,因此您需要将其更改为相对路径(假设它与main.js位于同一目录中):

require("./app.js");

Webpack遵循Node.js的导入行为,但它也允许您使用resolve.modules选项进行更改。