Webpack缺少模块'找不到模块'

时间:2016-01-16 04:54:57

标签: javascript webpack babeljs

我目前正在研究一个反应webpack babel等网站并试图第一次构建。构建成功,但是当我打开浏览器时出现以下错误:

Uncaught Error: Cannot find module "/Users/michael.nakayama/Documents/Development/jamsesh/node_modules/webpack/node_modules/node-libs-browser/node_modules/process/browser.js"

此模块存在。在浏览器中找到该实际网址会显示相关文件。但我无法弄清楚为什么webpack找不到它。我不知道这是babel6问题还是webpack问题,或者两者都不知道。我的配置文件如下所示:

var webpack = require('webpack');
var cleanWebpack = require('clean-webpack-plugin');

var ignore = new webpack.IgnorePlugin(new RegExp("/(node_modules|ckeditor)/"))

module.exports = {
  devtool: 'inline-source-map',
  entry: './lib/client/entry',
  output: {
    path: __dirname + '/public/js',
    filename: 'app.js',
    publicPath: 'http://localhost:8081/js/',
  },
  plugins: [
    ignore,
  ],
  resolve: {
    extensions: ['', '.js'],
    moduleDirectories: ['./node_modules']
  },
  module: {
    loaders: [
      {
        test: /\.js?$/,
        loaders: ['babel-loader?presets[]=react,presets[]=es2015,plugins[]=transform-es2015-classes,plugins[]=transform-react-jsx'],
        exclude: /(node_modules)/,
      }
    ]
  }
}

我的webpack服务器文件如下:

var WebpackDevServer = require('webpack-dev-server');

var webpack = require('webpack');
var config = require('../../webpack.config');

var server = new WebpackDevServer(webpack(config), {
  // webpack-dev-server options
  publicPath: config.output.publicPath,
  stats: { colors: true },
});

server.listen(8081, 'localhost', function() {});

以下是我安装的软件包:

"devDependencies": {
    "babel-cli": "^6.3.17",
    "babel-core": "^6.3.26",
    "babel-loader": "^6.2.0",
    "babel-plugin-syntax-jsx": "^6.3.13",
    "babel-plugin-transform-es2015-classes": "^6.4.0",
    "babel-plugin-transform-react-jsx": "^6.3.13",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "body-parser": "^1.14.2",
    "clean-webpack-plugin": "^0.1.5",
    "express": "^4.13.3",
    "history": "^1.17.0",
    "jade": "^1.11.0",
    "nodemon": "^1.8.1",
    "path": "^0.12.7",
    "pg": "^4.4.3",
    "react": "^0.14.6",
    "react-dom": "^0.14.3",
    "react-hot-loader": "^1.3.0",
    "react-router": "^1.0.3",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  }

entry.js:

var React = require('react');
var ReactDOM = require('react-dom');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var routes = require('../routes');

// -v x.13.x
/**Router.run(routes, Router.HistoryLocation, function (Handler, state) {
  React.render(<Handler/>, document.getElementById('react-app'));
});**/

var node = document.getElementById('react-app');
// -v 1.0.0
ReactDOM.render(<Router history={createBrowserHistory()} routes={routes}/> , node);

另外作为抬头,我尝试卸载并重新安装我的所有软件包。我尝试过专门安装node-libs-browser模块。感谢。

2 个答案:

答案 0 :(得分:11)

node_modules上忽略插件的问题。在webpack.config.js中,您有:

var ignore = new webpack.IgnorePlugin(new RegExp("/(node_modules|ckeditor)/"))
...
plugins: [
  ignore,
],

来自Ignore Plugin文档:

  

请勿为与提供的RegExp匹配的请求生成模块。

Webpack尝试使用名为require的{​​{1}}模块用于React模块,并因为它被忽略而失败。

尝试从Ignore插件中删除node_modules/process/browser,或者如果确实需要,请写下较少的全局条件。

答案 1 :(得分:1)

导入nodeExternals对我有用,这是我的server.webpack.config:

import path from 'path';
**import nodeExternals from 'webpack-node-externals';**

const CONTEXT = path.join( __dirname, "../.." ),
  INPUT_SERVER_DIR = path.join( CONTEXT, "server" ),
  OUTPUT_SERVER_DIR = path.join( CONTEXT, "dist/server" );

export default [
  {
    name: 'server',
    target:  'node',
    context: INPUT_SERVER_DIR,
    node: {
      __dirname: false
    },
    entry: './server',
    devtool : 'source-map',
    output: {
      path: OUTPUT_SERVER_DIR,
      filename: "server.js"
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          loader: "babel-loader"
        }
      ]
    },
    resolve: {
      extensions: ['.js']
    },
    **externals : [ nodeExternals() ]**
  }
];