执行 NPM 脚本时出现 SyntaxError

SyntaxError when NPM script is executed

我正在尝试更新 React 应用程序的依赖项。此应用包含一个在 package.json 中定义的脚本,它为每个语言环境生成一个消息包。

"scripts": {
  "build:langs": "NODE_ENV=production babel-node scripts/mergeMessages.js"
}

脚本的细节并不重要,但它与 this one, which is described in a react-intl tutorial 非常相似。

在升级依赖项之前,脚本可以运行,但现在当我在命令行上执行 npm run build:langs 时,出现此错误:

/applications/my-app/scripts/mergeMessages.js:1
import _objectSpread from "/applications/my-app/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/objectSpread";
       ^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Module._compile (/applications/my-app/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Object.newLoader [as .js] (/applications/my-app/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:824:10)
    at Object.<anonymous> (/applications/my-app/node_modules/@babel/node/lib/_babel-node.js:234:23)
    at Module._compile (internal/modules/cjs/loader.js:759:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)

我想 babel 依赖项是最相关的。升级前这些是:

"babel-core": "6.26.0",
"babel-eslint": "7.2.3",
"babel-loader": "7.1.2",
"babel-plugin-react-intl": "2.4.0",
"babel-preset-react-app": "3.1.2",
"babel-cli": "6.26.0",

升级后,babel 依赖项为:

"@babel/core": "7.4.4",
"@babel/polyfill": "7.4.4",
"@babel/register": "7.4.4",
"babel-eslint": "10.0.1",
"babel-loader": "8.0.5",
"babel-plugin-named-asset-import": "^0.3.2",
"babel-preset-react-app": "^8.0.0",
"babel-plugin-react-intl": "2.4.0",
"@babel/cli": "~7.4.4",
"@babel/node": "7.2.2",

更新

我没有 .babelrcbabel.config.js 配置文件,只有 package.json

中的以下内容
"babel": {
  "presets": [
    "react-app"
  ]
}

来自Babel 7 migration guide

The babel-node command in Babel 6 was part of the babel-cli package. In Babel 7, this command has been split out into its own @babel/node package, so if you are using that command, you'll want to add this new dependency.

如果您使用的是 babel-7,则需要具备以下条件:

尝试使用 @babel/preset-react 而不是 babel-preset-react-app

"@babel/core": "^7.4.4",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",

和babel-config在package.json应该是:

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