Javascript Webpack 缺少模块“未找到模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34823655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:55:00  来源:igfitidea点击:

Webpack Missing Module 'Module Not Found'

javascriptwebpackbabeljs

提问by Michael Nakayama

I'm currently working on a react webpack babel etc site and trying to build the first time. The build is successful, but when I open up the browser I get the following error:

我目前正在一个 react 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"

This module exists. Going to that actual url in my browser shows the file in question. But I cannot figure out why webpack cannot find it. I don't know if this is a babel6 issue or a webpack issue, or neither. My config file looks like this:

该模块存在。在我的浏览器中转到该实际 url 会显示有问题的文件。但我不明白为什么 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)/,
      }
    ]
  }
}

and my webpack server file is as follows:

我的 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() {});

and here are the packages I have installed:

这是我安装的软件包:

"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:

入口.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);

Also as a heads up, I have tried uninstalling and reinstalling all my packages. I have tried installing specifically the node-libs-browser modules. thanks.

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

采纳答案by Dmitry Yaremenko

The problem with ignore plugin on node_modules. In webpack.config.js, you have:

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

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

From Ignore Plugindocumentation:

忽略插件文档:

Don't generate modules for requests matching the provided RegExp.

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

Webpack tries to requiremodule with the name node_modules/process/browserfor React module and fails with it because it is ignored.

Webpack 尝试require使用node_modules/process/browserReact 模块的名称进行模块并失败,因为它被忽略了。

Try to remove node_modulesfrom Ignore Plugin or write less global condition if you really need this.

node_modules如果您确实需要,请尝试从忽略插件中删除或编写较少的全局条件。

回答by Davide Carpini

importing nodeExternals worked for me, this is my server.webpack.config:

导入 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() ]**
  }
];