首页 > 解决方案 > Babel Transpiler 无法与 Nodemon 一起运行

问题描述

问题:

无法将 Babel Transpiler 与 Nodemon 一起使用

细节:

在 package.json 我有:

"scripts": {
   "start": "nodemon --exec babel-node --presets=es2015 -- src/app.js"
},


"dependencies": {
   "nodemon": "^1.18.4"
},
"devDependencies": {
  "@babel/cli": "^7.1.2",
  "@babel/core": "^7.1.2",
  "@babel/preset-env": "^7.1.0",
  "babel-cli": "^6.26.0",
  "babel-preset-env": "^1.7.0"
}

当我运行npm start我的理解是 nodemon 应该启动保存并运行 babel 转译器;但是,我在终端中得到以下信息。

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

我认为这是由babel-cli6.26 的依赖引起的,但是当我删除它时,它会发出声音:

[nodemon] failed to start process, "babel-node" exec not found

通过好的 ol' google 机器搜索,我看到其他一些设置更复杂的人,他们的解决方案似乎在我的脑海中飞过。

娱乐步骤:

  1. 运行npm init -y&npm i nodemon

  2. 按照此处的说明进行操作:https ://babeljs.io/setup#installation (nodemon) selected

  3. npm install @babel/core --save-dev当我被警告说没有安装核心时运行。

  4. npm start

标签: node.jsnpmbabeljsnodemon

解决方案


使用 nodemon 运行 Babel 时,您需要包含这些包。

"devDependencies": {
  "@babel/cli": "^7.1.2",
  "@babel/core": "^7.1.2",
  "@babel/polyfill": "^7.0.0",
  "@babel/preset-env": "^7.1.0",
  "@babel/node": "^7.0.0",
  "nodemon": "^1.18.4"
}

然后将您的 npm 运行脚本调整为:

"start": "nodemon app/index.js --exec babel-node app/index.js"

感谢 Babel Slack 频道的回答!


推荐阅读