webpack:未捕获的ReferenceError:未定义require

时间:2017-08-22 13:25:23

标签: javascript reactjs webpack ecmascript-6 babeljs

在webpack target = node中出现此错误,但我已完成target=web(默认)

我也没有在外部加载reactjs

在浏览器中加载应用时出现此错误

我做错了什么?

在控制台中

enter image description here

文件

enter image description here enter image description here

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const nodeExternals = require('webpack-node-externals');

const config = {
    target: 'web',
    externals: [nodeExternals()],
    entry: './src/index.js',
    output: {
        filename: '[name].bundle.js',
        path: __dirname + '/build'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.(png|svg|jpe?g|gif)$/,
                use: [{
                        loader: 'file-loader',
                        options: {
                            name: '[path][name].[ext]'
                        }
                    }
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: ['file-loader']
            }
        ]
    },
    devtool: 'inline-source-map',
    plugins: [
        new HtmlWebpackPlugin({
            title: 'Instarem.com'
        })
    ]
};

module.exports = config;

.babelrc 使用

babel-preset-env

{
    "presets": [
        "react",
        [
            "env",
            {
                "targets": {
                    "browsers": ["last 2 versions"]
                },
                "debug": true,
                "modules": "commonjs"
            }
        ]
    ],
    "plugins": [
        "transform-object-rest-spread",
        "transform-class-properties"
    ]
}

谢谢:)

  

我找到了线索

在Facebook的 create react app generator 捆绑包中显示

module.exports = __webpack_require__(/*! ./lib/React */ "./node_modules/react/lib/React.js");

但在我的情况下它只显示

module.exports = require("react");

4 个答案:

答案 0 :(得分:11)

你不应该使用

externals: [nodeExternals()],

在网络应用中。根据{{​​3}},它只适用于后端。由于您在Web应用程序中使用nodeExternals,因此您需要获得CommonJS模块,这需要内置节点require功能。所以只需将其删除即可修复错误。

答案 1 :(得分:2)

我在本地设置了这个配置(减去看似特定于你的env的无关的东西)并且它有效。

<强>的package.json

{
  "name": "test-webpack",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.24.1",
    "webpack": "^3.5.5"
  },
  "dependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  }
}

<强> .babelrc

{
  "presets": [
    "react",
    [
      "env",
      {
        "targets": {
          "browsers": ["last 2 versions"]
        },
        "debug": true,
        "modules": "commonjs"
      }
    ]
  ]
}

<强> webpack.config.js

const config = {
  target: 'web',
  entry: './index.js',
  output: {
    filename: '[name].bundle.js',
    path: __dirname + '/build',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
    ],
  },
};
module.exports = config;

<强> index.js

import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<div>Hello</div>, document.getElementById('root'));

<强>的index.html

<body>
    <div id="root" />
    <script src=" ./build/main.bundle.js "></script>
</body>

运行npm start构建捆绑包并运行index.html运行react应用程序。

答案 2 :(得分:-1)

也许对某人会有帮助。将值“ umd”添加到通话的nodeExternals选项importType中。

Name           Grade

--------------------
John           100

Mike           94
...
...

答案 3 :(得分:-1)

如果您使用的是webpackHtmlWebpackPlugin,请尝试从<script src="./src/app.js"></script>上删除 index.html