问题来自@ .babel import _Object $ defineProperty from“ ../../ core-js / object / define-property”;

问题描述 投票:0回答:1

我正在尝试配置NodeJs项目。如果在代码中添加任何异步/等待,则会出现以下错误:

1] import _Object$defineProperty from "../../core-js/object/define-property";
[1]        ^^^^^^^^^^^^^^^^^^^^^^
[1] 
[1] SyntaxError: Unexpected identifier

我的.babelrc:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": true
        },
      }
    ]
  ],
  "plugins": ["@babel/plugin-transform-regenerator"]
}

我的webpack.config.js是

const path = require("path")
const nodeExternals = require("webpack-node-externals")

const config = {
  entry: "./src/index.js",
  target: "node",
  externals: [nodeExternals()], // in order to ignore all modules in node_modules folder

  output: {
    filename: "[name].js",
    publicPath: "/client/dist/",
    chunkFilename: "[name].bundle.js",
    path: `${__dirname}/client/dist`
  },
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: ["node_modules/"],
        include: [/src/],
        use: {
          loader: "babel-loader"
        },
        plugins: ["@babel/plugin-transform-runtime"]
      },
      {
        test: /\.(woff2?|eot|ttf|otf)$/,
        loader: "file-loader",
        options: {
          limit: 10000,
          name: "[name].[hash:7].[ext]"
        }
      }
    ]
  },
  watch: true
}

module.exports = config

我试图添加@ babel / plugin-transform-runtime,但是它不能解决我的问题。有人可以给我一个提示,我在做什么不对?

提前感谢

node.js webpack babel
1个回答
0
投票

尝试将babel-polyfill添加到您的项目中,并作为webpack的条目:

yarn add babel-polyfill -D

webpack.config.js

const path = require("path")
const nodeExternals = require("webpack-node-externals")

const config = {
  entry: {
    polyfill: "babel-polyfill",
    app: "./src/index.js"
  },

...
© www.soinside.com 2019 - 2024. All rights reserved.