Babel.js webpack:未捕获引用错误:未定义require

qybjjes1  于 2022-12-16  发布在  Babel
关注(0)|答案(6)|浏览(349)

在webpack target = node中出现此错误,但我已执行target=web(默认)
我也没有从外部加载React
这个错误来在加载应用程序在浏览器中

我做错了什么
在控制台中

文件

第一节第一节第一节第二节第一节

网络包配置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"
    ]
}


谢谢:)
"我找到了线索"

module.exports = __webpack_require__(/*! ./lib/React */ "./node_modules/react/lib/React.js");
  • 但在我的情况下,它只显示 *
module.exports = require("react");
ep6jt1vc

ep6jt1vc1#

您不应使用

externals: [nodeExternals()],

根据https://github.com/liady/webpack-node-externals,它仅用于后端。由于您在Web应用中使用nodeExternals,因此您将获得CommonJS模块,该模块预期内置节点require函数。因此,只需删除它即可修复错误。

oipij1gg

oipij1gg2#

我的问题在我的package.json中,我必须删除
"type":"module"
它尝试将文件作为es6模块加载
https://nodejs.org/api/packages.html#packages_type

cotxawn7

cotxawn73#

也许这对某些人会有帮助。将值为“umd”的importType添加到调用nodeExternals选项中。

nodeExternals({
   importType: 'umd'
})
u5i3ibmn

u5i3ibmn4#

几分钟前我遇到了这个问题。这通常发生在你混淆了webpack.config文件中bundle的target属性时。例如,如果你正在为React创建一个bundle,目标应该是target: 'web',并且不应该包括任何与'async-node''node'等节点相关的内容。

const reactConfig = {
   name: 'React Bundle',
   target: 'web',
   ...
}
gcuhipw9

gcuhipw95#

我在本地设置了这个配置(减去似乎特定于您的env的无关的东西),它工作了。

包.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"
  }
}

.巴伯尔中心

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

网络包配置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;

索引.js

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

索引.html

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

运行npm start构建了bundle,运行index.html运行react应用程序。

vuktfyat

vuktfyat6#

如果您正在使用webpackHtmlWebpackPlugin,请尝试index.html中删除<script src="./src/app.js"></script>

相关问题