我正在尝试在Node.js中动态导入webpack
if (condition) {
import('webpack').then(webpack => webpack);
}然而,在我的终端中,我看到了以下错误:
C:\Users\myUser\react\node_modules\@babel\core\lib\transformation\normalize-file.js:209
throw err;
^
SyntaxError: C:\Users\myUser\react\server\index.js: Support for the experimental syntax 'dynamicImport' isn't currently enabled (23:3):
19 |
20 | if (condition) {
> 21 | import('webpack').then(webpack => webpack);
| ^
22 |
Add @babel/plugin-syntax-dynamic-import (https://git.io/vb4Sv) to the 'plugins' section of your Babel config to enable parsing.我在我的.babelrc中安装了@babel/plugin-syntax dynamic-import
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-class-properties"
]
}我甚至尝试将它添加到带有加载程序"babel- loader“的.js的规则下的webpack配置文件中。
我在试着避开CmJS
const webpack = require('webpack');在任何情况下,我都会收到相同的错误,并且找不到解决方案。有没有人经历过这个?谢谢
发布于 2019-04-26 14:48:01
向.babelrc添加plugins: ["dynamic-import-webpack"]
还可以安装plug $npm i babel-plugin-dynamic-import-webpack --D
https://stackoverflow.com/questions/53641773
复制相似问题