vij*_*jay 12 javascript ecmascript-6 reactjs webpack babeljs
在webpack中出现此错误,target = node
但我已经完成target=web
(默认)
我也没有在外部加载reactjs
在浏览器中加载应用程序时出现此错误
我做错了什么?
在控制台中
文件
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;
Run Code Online (Sandbox Code Playgroud)
.babelrc使用
babel-preset-env
{
"presets": [
"react",
[
"env",
{
"targets": {
"browsers": ["last 2 versions"]
},
"debug": true,
"modules": "commonjs"
}
]
],
"plugins": [
"transform-object-rest-spread",
"transform-class-properties"
]
}
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
我找到了线索
在Facebook的创建反应应用程序生成器包它显示
module.exports = __webpack_require__(/*! ./lib/React */ "./node_modules/react/lib/React.js");
Run Code Online (Sandbox Code Playgroud)
但在我的情况下它只显示
module.exports = require("react");
Run Code Online (Sandbox Code Playgroud)
Max*_*min 17
你不应该使用
externals: [nodeExternals()],
Run Code Online (Sandbox Code Playgroud)
在网络应用程序 根据https://github.com/liady/webpack-node-externals,它仅适用于后端.由于您nodeExternals
在Web应用程序中使用了CommonJS模块,因此需要内置节点require
功能.所以只需删除它来修复错误.
我的问题出在我的 package.json 中,我必须删除
"type":"module"
它试图将文件作为 es6 模块加载
https://nodejs.org/api/packages.html#packages_type
小智 7
几分钟前我遇到了这个问题。当您在webpack.configtarget
文件中混淆捆绑包的属性时,通常会发生这种情况。例如,如果您正在为 React 创建捆绑包,则目标应该并且不应该包含与节点相关的任何内容,例如或。target: 'web'
'async-node'
'node'
const reactConfig = {
name: 'React Bundle',
target: 'web',
...
}
Run Code Online (Sandbox Code Playgroud)
小智 6
也许这会对某人有帮助。添加到调用 nodeExternals 选项 importType ,值为“umd”。
nodeExternals({
importType: 'umd'
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14641 次 |
最近记录: |