节点构建因babel失败而引发错误:需要Babel“ ^ 7.0.0-0”,但已加载“ 6.26.0”

Pra*_*nay 3 node.js babeljs

我正在尝试使用babel构建节点应用程序,但会引发错误。

错误:需要Babel“ ^ 7.0.0-0”,但已加载“ 6.26.0”。如果您确定拥有@ babel / core的兼容版本,则可能是您的构建过程中正在加载错误的版本。检查此错误的堆栈跟踪,以查找第一个未提及“ @ babel / core”或“ babel-core”的条目,以了解正在调用的Babel。

at throwVersionError (E:\mern-master\node_modules\@babel\helper-plugin-utils\lib\index.js:65:11)
at Object.assertVersion (E:\mern-master\node_modules\@babel\helper-plugin-utils\lib\index.js:13:11)
at _default (E:\mern-master\node_modules\@babel\plugin-proposal-object-rest-spread\lib\index.js:41:7)
at E:\mern-master\node_modules\@babel\helper-plugin-utils\lib\index.js:19:12
at Function.memoisePluginContainer (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:113:13)
at Function.normalisePlugin (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:146:32)
at C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:184:30
at Array.map (<anonymous>)
at Function.normalisePlugins (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:158:20)
at OptionManager.mergeOptions (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:234:36)
at OptionManager.init (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:368:12)
at File.initOptions (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\index.js:212:65)
at new File (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\index.js:135:24)
at Pipeline.transform (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
at transform (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\util.js:50:22)
at Object.compile (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\util.js:59:12)
at write (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\dir.js:21:21)
at handleFile (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\dir.js:43:7)
at C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\dir.js:61:9
at Array.forEach (<anonymous>)
at handle (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\dir.js:59:29)
at Array.forEach (<anonymous>)
at module.exports (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\dir.js:69:15)
at Object.<anonymous> (C:\Users\PranayKumar\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\index.js:129:1)
at Module._compile (internal/modules/cjs/loader.js:702:30)
Run Code Online (Sandbox Code Playgroud)

npm ERR!代码ELIFECYCLE npm ERR!errno 1 npm错误!mern-ecommerce@1.0.0版本:mkdir build && babel server/ -d build/server --ignore ./server-entry.js npm错误!退出状态1 npm ERR!npm ERR!在mern-ecommerce@1.0.0生成脚本处失败。npm ERR!npm可能不是问题。上面可能还有其他日志记录输出。

npm ERR!可以在以下位置找到该运行的完整日志:npm ERR!C:\ Users \ PranayKumar \ AppData \ Roaming \ npm-cache_logs \ 2018-09-22T18_07_48_781Z-debug.log

以下是我的文件。

package.json

  "scripts": {
    "start": "pm2 start ./pm2-prod.json",
    "build": "mkdir build && babel src/server/ -d build/server --ignore ./src/server/server-entry.js",
    "server": "pm2 start ./pm2-dev.json --no-daemon",
    "build-webpack": "webpack --config webpack.config.client.js",
    "client": "webpack-dev-server --config webpack.config.client.js --open"
  },
  "devDependencies": {
    "@babel/core": "^7.1.0",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-proposal-decorators": "^7.0.0",
    "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
    "@babel/plugin-proposal-function-sent": "^7.0.0",
    "@babel/plugin-proposal-json-strings": "^7.0.0",
    "@babel/plugin-proposal-numeric-separator": "^7.0.0",
    "@babel/plugin-proposal-throw-expressions": "^7.0.0",
    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
    "@babel/plugin-syntax-import-meta": "^7.0.0",
    "@babel/polyfill": "^7.0.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "^7.0.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5"
  }
Run Code Online (Sandbox Code Playgroud)

服务器端.babelrc

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "plugins": [
        "@babel/plugin-proposal-object-rest-spread"
    ]
}
Run Code Online (Sandbox Code Playgroud)

server-entry.js

require('@babel/register');
require('./server.js');
Run Code Online (Sandbox Code Playgroud)

log*_*yth 5

如堆栈跟踪中所示,通过Babel正在调用Babel babel-cli,这是一个问题,因为babel-cliBabel 6.x CLI是。您babel-clidevDependencies清单中也没有任何内容。

对于Babel 7,您需要:

  • 安装 @babel/cli
  • 确保删除任何babel-cli已经存在的已安装版本
  • 确保实际上是使用babel本地项目中的命令而不是全局安装的版本来编译文件。