【问题标题】:Dynamically import webpack in Node.js在 Node.js 中动态导入 webpack
【发布时间】:2018-12-05 22:31:51
【问题描述】:

我正在尝试在 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 conf 文件中。

我试图避免 CmJS

const webpack = require('webpack');

在任何情况下,我都会收到相同的错误,但我找不到解决方案。有人经历过吗?谢谢

【问题讨论】:

标签: node.js webpack babeljs dynamic-import


【解决方案1】:

plugins: ["dynamic-import-webpack"] 添加到.babelrc

同时安装插件$npm i babel-plugin-dynamic-import-webpack --D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2019-06-07
    • 2019-06-16
    相关资源
    最近更新 更多