简体   繁体   English

带有 JS 装饰器(第 3 阶段)和 Babel 的 NodeJS JavaScript 项目不起作用

[英]NodeJS JavaScript project with JS decorators (stage 3) and Babel doesn't work

I am trying to use JavaScript decorators (stage 3 proposal) in my NodeJS project, which is written in JavaScript. However, I am having trouble getting this to work with nodemon and babel together, so that the code will be recompiled when it is changed.我正在尝试在我的 NodeJS 项目中使用JavaScript 装饰器(第 3 阶段提案) ,该项目是在 JavaScript 中编写的。但是,我无法将其与 nodemon 和 babel 一起使用,因此代码将在更改时重新编译.

I tried two different package.json configurations:我尝试了两种不同package.json配置:

First one:第一:

"scripts": {
    "build": "babel src -d dist",
    "start": "npm run build && nodemon dist/app.js"
  },

It works the first time only, when I made a change in code it recompiles but I see that runtime is not updated, I see the old result.它只在第一次工作,当我对代码进行更改时它会重新编译但我看到运行时没有更新,我看到的是旧结果。

Second configuration:第二个配置:

"scripts": {
    "start-nodemon": "nodemon --exec babel-node src/app.js"
  },

It doesn't work with decorators and I get the following exception:它不适用于装饰器,我得到以下异常:

 @test(true)
    ^

SyntaxError: Invalid or unexpected token

When I remove the decorator it works.当我删除装饰器时它起作用了。

My .babelrc config is following:我的.babelrc配置如下:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false
      }
    ]
  ],
  "env": {
    "development": {
      "sourceMaps": "inline",
      "retainLines": true
    }
  },
  "plugins": [
    "@babel/plugin-syntax-top-level-await",
    ["@babel/plugin-proposal-decorators", {
      "version": "2022-03"
    }]
  ]
}

app.js file应用程序app.js文件

import MyModule from "./module.js";

const initializedModule = new MyModule();
const description = initializedModule.getDescription();
console.log(description);

decorators.js file decorators.js文件

export function test(test){
    return function (value, { kind, name }) {
        if (kind === "method" || kind === "getter" || kind === "setter") {
            return function (...args) {
                console.log(test);
                console.log(`starting ${name} with arguments ${args.join(", ")}`);
                const ret = value.call(this, ...args);
                console.log(`ending ${name}`);
                return ret;
            };
        }
    }
}

and module.js file:module.js文件:

import {test} from "./decorators.js";

export default class MyModule {
    @test(true)
    getDescription () {
        return "MyModule Test12";
    }
}

How can I properly configure my project, so code will be recompiled and rerun upon changes?我怎样才能正确配置我的项目,以便在更改时重新编译并重新运行代码?

GitHub repository with my project: babel-test GitHub 存储库与我的项目: babel-test

Thank you!谢谢!

Try removing @babel/node and replacing it with babel-register-esm :尝试删除 @babel/node 并将其替换为babel-register-esm

npm uninstall @babel/core && npm install --save-dev babel-register-esm

Then modify the package.json file, the scripts section:然后修改package.json文件,scripts部分:

...
"scripts": {
      "build": "babel src -d dist",
      "start": "yarn build && node --experimental-specifier-resolution=node dist/app.js",
      "start-nodemon": "nodemon --watch \"./src/**/*.js\" --ignore \"node_modules\" --exec \"node --loader babel-register-esm src/app.js\""
   },
...

This part这部分

node --loader babel-register-esm src/app.js

is the most important.是最重要的。 It changes the node loader to one that can process ESM features.它将节点加载器更改为可以处理 ESM 功能的节点加载器。

And this part:这部分:

node --loader babel-register-esm src/app.js

makes it possible to start a the ESM files.使启动 ESM 文件成为可能。

The expected result is:预期结果是:

starting getDescription with arguments 
ending getDescription
MyModule Test12

I used yarn instead of npm, and my package.json is:我用纱线代替 npm,我的 package.json 是:

{
 "name": "fixstack",
 "version": "1.0.0",
 "main": "dist/app.js",
 "license": "MIT",
 "type": "module",
 "scripts": {
   "build": "babel src -d dist",
   "start": "yarn build && node --experimental-specifier-resolution=node dist/app.js",
   "start-nodemon": "nodemon --watch \"./src/**/*.js\" --ignore \"node_modules\" --exec \"node --loader babel-register-esm src/app.js\""
 },
   "dependencies": {},
   "devDependencies": {
      "@babel/cli": "^7.20.7",
      "@babel/core": "^7.20.12",
      "@babel/plugin-proposal-decorators": "^7.20.7",
      "@babel/preset-env": "^7.20.2",
      "babel-register-esm": "^1.2.4",
      "nodemon": "^2.0.20"
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM