首页 > 解决方案 > NPM 包中的类。当前未启用对实验语法“classProperties”的支持

问题描述

我创建了一个 npm 包,其中一个库包括“Employee.js”,如下所示:

class Employee{
    name = null
}

在我放的 npm 包的 package.json 中

  "babel": {
    "presets": [
      "@babel/preset-env"
    ],
    "plugins": [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ]
  },

我有这些 babel 依赖项

  "devDependencies": {
    "@babel/cli": "^7.13.0",
    "@babel/core": "^7.13.8",
    "@babel/plugin-proposal-class-properties": "^7.13.0",
    "@babel/preset-env": "^7.13.9"
  }

每当我尝试在电子中运行它时,我都会收到此错误


./node_modules/xxx/xxx/Employee.js
Support for the experimental syntax 'classProperties' isn't currently enabled:

> 6 |     name = null


Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-class-properties (https://git.io/vb4yQ) to the 'plugins' section to enable parsing.

如何制作它以便我可以在文件中拥有类定义并且在导入包时不会导致此问题?

谢谢你。

标签: javascriptnode.jsnpmelectronbabeljs

解决方案


推荐阅读