2016-09-04 56 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

相關問題