找不到模块:错误:无法解析'webpack'

时间:2017-02-21 10:23:26

标签: javascript node.js webpack webpack-2

我正在尝试使用我拥有的所有javascript生成bundle.js。这是我用过的命令。

> C:\Users\aaa\node_modules\.bin>"C:\Users\aaa\node_modules\.bin\webpack.cmd" "D:\www\webpack.config.js" "D:\www\bundle.js"   

bundle.js已生成但构建失败。这是错误。

C:\Users\aaa\node_modules\.bin>C:\Users\aa\node_modules\.bin\webpack.cmd D:\www\webpack.config.js D:\www\bundle.js
Hash: 44eb203978bf482447b4
Version: webpack 2.2.1
Time: 94ms
    Asset   Size  Chunks             Chunk Names
bundle.js  15 kB       0  [emitted]  main
   [0] ../webpack/~/node-libs-browser/~/path-browserify/index.js 6.18 kB {0} [bu
ilt]
   [1] ../webpack/~/node-libs-browser/~/process/browser.js 5.3 kB {0} [built]
   [2] D:/www/webpack.config.js 567 bytes {0} [built]

ERROR in D:/www/webpack.config.js
Module not found: Error: Can't resolve 'webpack' in 'D:\www'
 @ D:/www/webpack.config.js 2:16-34   

这是我的webpack.config.js

const path = require('path');
const webpack = require('webpack');
module.exports = {
  context: path.resolve("www", './src'),
  entry: {
    app: ['js/index.js', './js/require.js', './js/google-protobuf.js', './js/proto/ByteBufferAB.min.js',
     './js/proto/Long.min.js', './js/proto/control_1.js', './js/proto/data_1.js', './js/proto/data_hmi_1.js',
      './js/proto/main_1.js'],
  },
  output: {
    path: path.resolve("www", './dist'),
    filename: 'bundle.js',
  }
};

请提供任何参考或提示。

2 个答案:

答案 0 :(得分:0)

尝试更改上下文和output.path:

context: path.resolve(__dirname, './'),
output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'bundle.js',
    publicPath: '/'
},

然后__dirname将替换为文件所在的物理路径。

答案 1 :(得分:0)

请检查您是否在全球范围内安装了网络包。

要检查的命令:webpack -v

此外,您应该执行webpack build命令: webpack --config webpack.config.js --progress --colors

请注意,命令应该在webpack.config.j存在的同一目录中执行。

根据您当前的命令使用本地webpack。