【发布时间】:2020-11-03 00:03:23
【问题描述】:
我正在尝试在我的本地机器上运行一个 node express 应用程序,以便使用 babel-node 进行动态转换。我创建了一个 server.js 入口点文件,其中包含以下代码:
require("@babel/register")();
// Import the rest of our application.
module.exports = require('./bin/www');
这是我用来启动应用程序的 package.json 脚本条目:
"testLocal": "nodemon --verbose --exec babel-node server.js --inspect=0.0.0.0:56746"
我在项目根目录添加了一个 .babelrc 文件,代码如下:
{
"presets": ["@babel/preset-env"]
}
问题是我继续收到以下错误:
import cluster from 'cluster';
^^^^^^
SyntaxError: Unexpected token import
这是我的 package-json 文件中的包,我删除了一些以将代码精简到最低限度的示例:
"@babel/core": "^7.11.6",
"@babel/node": "^7.12.1",
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
"@babel/plugin-proposal-optional-chaining": "^7.11.0",
"@babel/plugin-transform-async-to-generator": "^7.12.1",
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/polyfill": "^7.11.5",
"@babel/preset-env": "^7.12.1",
"@babel/register": "^7.11.5",
我在几篇不同的文章中找到了这个例子,但它似乎没有转译。我在我的本地机器上使用 babel-node,这样我就可以即时转换。我在此设置中遗漏了哪些可能导致此问题的内容?
【问题讨论】:
-
试试
babel-node ./bin/www而不是server.js
标签: javascript node.js babeljs