0

我在 Heroku 上将 babel-node 用于 ES6 语法,但在部署时出现构建错误。它说“babel-node:未找到”。我已经尝试在本地计算机上进行全新安装,并且效果很好。包.json:

{
  "name": "secret",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "./node_modules/.bin/babel-node ./bin/www"
  },
  "engines": {
    "node": "7.5.0",
    "npm": "5.0.3"
  },
  "dependencies": {
    "babel-cli": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    ...
  }
}

我感谢您的帮助!:)

2017-07-17T11:02:26.997876+00:00 heroku[web.1]: State changed from crashed to starting
2017-07-17T11:02:31.839278+00:00 heroku[web.1]: Starting process with command `npm start`
2017-07-17T11:02:38.074157+00:00 heroku[web.1]: State changed from starting to crashed
2017-07-17T11:02:38.062607+00:00 heroku[web.1]: Process exited with status 1
2017-07-17T11:02:37.865774+00:00 app[web.1]:
2017-07-17T11:02:37.865786+00:00 app[web.1]: > secret@0.0.0 start /app
2017-07-17T11:02:37.865787+00:00 app[web.1]: > babel-node ./bin/www
2017-07-17T11:02:37.865787+00:00 app[web.1]:
2017-07-17T11:02:37.936361+00:00 app[web.1]: sh: 1: babel-node: not found
2017-07-17T11:02:37.966452+00:00 app[web.1]: npm ERR! file sh
2017-07-17T11:02:37.966683+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-07-17T11:02:37.966931+00:00 app[web.1]: npm ERR! errno ENOENT
2017-07-17T11:02:37.967106+00:00 app[web.1]: npm ERR! syscall spawn
2017-07-17T11:02:37.967287+00:00 app[web.1]: npm ERR! secret@0.0.0 start: `babel-node ./bin/www`
2017-07-17T11:02:37.967430+00:00 app[web.1]: npm ERR! spawn ENOENT
2017-07-17T11:02:37.967588+00:00 app[web.1]: npm ERR!
2017-07-17T11:02:37.967733+00:00 app[web.1]: npm ERR! Failed at the secret@0.0.0 start script.
2017-07-17T11:02:37.967873+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-07-17T11:02:37.969151+00:00 app[web.1]:
2017-07-17T11:02:37.969374+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-07-17T11:02:37.969484+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2017-07-17T11_02_37_957Z-debug.log
2017-07-17T11:25:52.044791+00:00 heroku[web.1]: State changed from crashed to starting
2017-07-17T11:25:54.841341+00:00 heroku[web.1]: Starting process with command `npm start`
2017-07-17T11:25:57.407691+00:00 heroku[web.1]: Process exited with status 1
2017-07-17T11:25:57.413873+00:00 heroku[web.1]: State changed from starting to crashed
2017-07-17T11:25:57.310176+00:00 app[web.1]:
2017-07-17T11:25:57.310190+00:00 app[web.1]: > secret@0.0.0 start /app
2017-07-17T11:25:57.310191+00:00 app[web.1]: > babel-node ./bin/www
2017-07-17T11:25:57.310192+00:00 app[web.1]:
2017-07-17T11:25:57.316940+00:00 app[web.1]: sh: 1: babel-node: not found
2017-07-17T11:25:57.335019+00:00 app[web.1]: npm ERR! file sh
2017-07-17T11:25:57.335287+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-07-17T11:25:57.335517+00:00 app[web.1]: npm ERR! errno ENOENT
2017-07-17T11:25:57.335735+00:00 app[web.1]: npm ERR! syscall spawn
2017-07-17T11:25:57.335933+00:00 app[web.1]: npm ERR! secret@0.0.0 start: `babel-node ./bin/www`
2017-07-17T11:25:57.336095+00:00 app[web.1]: npm ERR! spawn ENOENT
2017-07-17T11:25:57.336280+00:00 app[web.1]: npm ERR!
2017-07-17T11:25:57.336449+00:00 app[web.1]: npm ERR! Failed at the secret@0.0.0 start script.
2017-07-17T11:25:57.336614+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-07-17T11:25:57.337985+00:00 app[web.1]:
2017-07-17T11:25:57.338232+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-07-17T11:25:57.338379+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2017-07-17T11_25_57_323Z-debug.log
4

2 回答 2

1

babel-node is "not meant for production use". There is a more relevant guide here for using @babel/cli, @babel/core and @babel/preset-env.

The babel dependencies should all be installed as devDependencies and you should transpile your code during in a build script run before Heroku prunes the devDependencies. See this guide

If you move all of your es6 code into a common directory like src at the top level of your app, you can create a build script to transpile everything in that directory to an output directory like build.

"build": "npm run clean && npm run build-babel",
"build-babel": "./node_modules/.bin/babel -d ./build ./src",
"clean": "rm -rf build && mkdir build",

In the example above, the build script will be run automatically by Heroku. clean will create a new build directory, and build-babel will transpile the code and place it in the build directory. After this is completed, Heroku will prune the devDependencies, but you will no longer need Babel.

When Heroku calls start below, it will be running your transpiled code. You should no longer see sh: 1: babel-node: not found.

"scripts": {
  "build": "npm run clean && npm run build-babel",
  "build-babel": "./node_modules/.bin/babel -d ./build ./src",
  "clean": "rm -rf build && mkdir build",
  "start": "node ./build/bin/www"
},

One potential gotcha, Do not call build from your start script. If you do, the devDependencies will have been pruned and you will get the following

sh: 1: babel: not found

于 2021-02-28T21:15:32.207 回答
0

有点晚了,但我认为你缺少的是:

步骤(1):npm install --save babel-cli babel-preset-env在终端

第 1 步的注意事项:不要保存到 dev,因为这需要您设置 NPM_CONFIG_PRODUCTION=false

第 (2) 步:如果不添加 babel 样板和/或使用 babelrc 文件,则不会将任何内容编译到 babel。最简单的方法是在您的主应用程序文件夹中创建一个 .babelrc 文件,即。与您的 package.json 所在的相同并输入以下内容:

 {
  "presets": ["env"]
}

第 2 步的注意事项:使用 env 预设,您不需要 babel-preset-es2015 等

步骤(3):您现在可以使用 babel-node 运行您的代码,因此在您的情况下执行您的启动脚本,即。./node_modules/.bin/babel-node ./bin/www

如果您需要进一步指导了解预设逻辑,我建议您阅读 NPM 模块:https ://www.npmjs.com/package/babel-preset-env

此外,我建议使用像 pm2 这样的流程管理器。这允许您的节点实例在崩溃时保持活动状态。永远的模块曾经这样做,但 pm2 更好。这是 package.json 中的启动脚本示例:

"scripts": { "start": "node ./node_modules/.bin/pm2 start main.json --attach --env production" }

Procfile(注意,我使用的是 nginx,但 npm start 可以正常工作):

web: bin/start-nginx npm start 
worker: node ./node_modules/.bin/pm2 start worker.json --attach

这将是您的带有 args 的 main.json 以使用 babel-node(注意,将实例保持为 1,并且不要在 Heroku 中的 web dyno 上分叉):

主要的.json:

{
  "name": "the_awesome",
  "script": "app.js",
  "instances": 1,
  "exec_mode": "cluster"
}

工人.json:

{
  "name": "worker",
  "script": "/app/lib/worker.js"
}

(4) 如果您在集群模式下使用 pm2,请在您的 app.js 和 worker.js 中添加一个 require('babel-register') 挂钩。如果你在集群模式下使用 pm2,或者只是不使用 pm2,那么你可以直接使用 babel-node 来运行你的 .js 或 es6 文件

最后,要检查是否真的安装了 babel-node,您可以执行以下操作:

 heroku run bash -a {name of your app}
 ls ./node_modules/.bin //babel-node should be present

祝你好运!

于 2018-02-20T19:00:45.390 回答