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

时间:2018-09-22 16:43:29

标签: node.js babeljs

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

  

错误:需要Babel“ ^ 7.0.0-0”,但已加载“ 6.26.0”。如果你   确保您具有@ babel / core的兼容版本,这很可能   您的构建过程中某些东西正在加载错误的版本。   检查此错误的堆栈跟踪以查找第一个条目   没有提及“ @ babel / core”或“ babel-core”来查看正在调用的内容   巴别塔。

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)

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

npm错误!可以在以下位置找到此运行的完整日志: 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"
  }

服务器端.babelrc

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "plugins": [
        "@babel/plugin-proposal-object-rest-spread"
    ]
}

server-entry.js

require('@babel/register');
require('./server.js');

1 个答案:

答案 0 :(得分:2)

如堆栈跟踪中所示,通过babel-cli调用Babel,这是一个问题,因为babel-cli是Babel 6.x CLI。您的babel-cli列表中也没有devDependencies

对于Babel 7,您需要:

  • 安装@babel/cli
  • 确保您删除了已经存在的所有已安装的babel-cli版本
  • 确保您实际上是通过babel命令从本地项目(而不是全局安装的版本)中编译文件的。