【问题标题】:babel-node: Syntax Error: Unexpected token import using node.jsbabel-node:语法错误:使用 node.js 导入意外的令牌
【发布时间】: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


【解决方案1】:

这是我的package.json 我正在使用cross-env 处理环境,您可以使用dotenv ... 和pinopino-pretty 处理日志。

"scripts": {
    "nobuildprod": "pm2 startOrRestart ecosystem.config.js --env production",
    "dev": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon --exec ./node_modules/.bin/babel-node --trace-warnings src/index.js  | pino-pretty",
    "build": "./node_modules/.bin/babel src --out-dir ./dist --source-maps --copy-files",

  ...

  "dependencies": {
    "@babel/runtime": "^7.11.2",
    "pino": "^6.7.0",
    "pino-http": "^5.3.0",
    "pino-pretty": "^4.3.0",
  
  ...


  "devDependencies": {
    "@babel/cli": "^7.11.6",
    "@babel/core": "^7.11.6",
    "@babel/node": "^7.10.5",
    "@babel/plugin-proposal-decorators": "^7.10.5",
    "@babel/plugin-transform-runtime": "^7.11.5",
    "@babel/preset-env": "^7.11.5",
    "@babel/register": "^7.11.5",

server.js

require('@babel/register');
require('./src/index.js');

.babelrc(我正在使用装饰器)

{
    "presets": [
        ["@babel/preset-env"]
    ],
    "plugins": [
        ["@babel/transform-runtime"],
        ["@babel/plugin-proposal-decorators", {"legacy" : true}]
    ],
    "env": {
        "development": {
          "sourceMaps": true,
          "retainLines": true
        }
    }
}

和生态系统.config.js

module.exports = {
  apps: [
    {
      name: 'myapp',
      script: 'server.js',
      instances: 'max',
      env: {
        NODE_ENV: 'development',
      },
      env_development: {
        NODE_ENV: 'development',
      },
      env_test: {
        NODE_ENV: 'test',
      },
      env_production: {
        NODE_ENV: 'production',
      },
    },
  ],
};

所以我的源文件位于./src 我可以运行这个应用程序进行开发,使用 pm2 进行生产,也可以轻松制作 docker。 希望对你有帮助。

【讨论】:

  • 虽然我真的很喜欢你的方法,但我还是明白了。我正在从 Docker 迁移到 pm2。在 Docker 中调试很痛苦。您是否在 index.js 文件中使用了任何导入语句?
  • 登录docker不痛苦,用docker logs -f <conrainerName or ID>是的,我用的是BABEL!
  • @user1790300 你的nodejs 版本是什么?
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多