2016-09-04 67 views
0

我在反应项目中工作。当我使用“exclude:/ node_modules”时,我的项目并未排除node_modules。所以我读了一些解决的问题,并将下面的代码添加到我的webpack.config.js中。在浏览器控制台中使用“externals”排除node_modules错误

var nodeExternals = require('webpack-node-externals'); 
... 
module.exports = { 
... 
target: 'node', // in order to ignore built-in modules like path, fs,  etc. 
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder 
... 
}; 

我webpack.config.js则看起来像这样

var path = require('path'); 
var webpack = require('webpack'); 
var nodeExternals = require('webpack-node-externals'); 

module.exports = { 
    devtool: 'eval', 
    entry: [ 
     'webpack-dev-server/client?http://localhost:3000', 
     'webpack/hot/only-dev-server', 
     './dev/js/index.js' 
    ], 
    target: 'node', // in order to ignore built-in modules like path, fs, etc. 
    externals: [nodeExternals()], // in order to ignore all modules in node_modules 
    module: { 

     loaders: [ 
      { 
       test: /\.js$/, 
       loaders: ['react-hot', 'babel'], 
       exclude: /node_modules/ 
      }, 
      { 
       test: /\.scss/, 
       loader: 'style-loader!css-loader!sass-loader' 
      } 
     ] 
    }, 
    output: { 
     path: path.join(__dirname, 'dist'), 
     filename: 'bundle.js', 
     publicPath: '/static/' 
    }, 
    plugins: [ 
     new webpack.HotModuleReplacementPlugin() 
    ] 
}; 

当我运行我的应用程序,我在控制台收到此错误

Browser Console

我怎样才能解决这个问题错误?

回答

0

我认为在documentationwebpack-node-externals线路:

当的WebPack捆绑的后端

这个库是意味着用于捆绑,在运行浏览器。仅在节点上运行的后端套件。

不包括node_modules在您的加载器应该足以排除未引用的node_modules。否则,你需要引用的node_modules被包含在bundle中。

对于浏览器,您只需排除已存在于页面上的模块。例如,如果您正在从cdn提取反应,这可能很有用。而且你不希望在应用程序包中包含所有react.js。

+0

所以我应该删除它 – ApurvG

+0

是的,你不应该需要这个模块的浏览器包。 –

+0

但是为什么浏览器控制台热载入程序会说更新了576个模块,每当我对组件进行更改时。为什么每次更新很多模块?这意味着节点模块每次都会更新。我应该怎么做? – ApurvG

相关问题