我在Heroku上使用babel-node for ES6语法,但是在部署时遇到了构建错误。它说" babel-node:not found"。我在本地计算机上尝试过全新安装,但效果很好。的package.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
答案 0 :(得分:0)
有点晚了,但这就是我认为你缺少的东西:
步骤(1):终端中的npm install --save babel-cli babel-preset-env
关于步骤1的注意事项:不要保存到dev,因为这将要求您设置NPM_CONFIG_PRODUCTION = false
步骤(2):如果没有添加babel样板文件或使用babelrc文件,则不会编译任何内容。最简单的方法是在主app文件夹中创建.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中的启动脚本示例:
"脚本":{ " 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
这将是你的main.json与args使用babel-node(注意,将实例保持为1并且不要在Heroku中的web dyno上分叉):
main.json:
{
"name": "the_awesome",
"script": "app.js",
"instances": 1,
"exec_mode": "cluster"
}
worker.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
祝你好运!
答案 1 :(得分:0)
babel-node
是 "not meant for production use"。有一个更相关的指南 here 使用 @babel/cli
、@babel/core
和 @babel/preset-env
。
babel 依赖项都应该作为 devDependencies 安装,并且你应该在 Heroku 修剪 devDependencies 之前在 build
脚本中转译你的代码。见this guide
如果您将所有 es6 代码移动到应用顶级的公共目录(例如 src
)中,您可以创建构建脚本将该目录中的所有内容转换为输出目录(例如 {{1} }.
build
在上面的示例中,"build": "npm run clean && npm run build-babel",
"build-babel": "./node_modules/.bin/babel -d ./build ./src",
"clean": "rm -rf build && mkdir build",
脚本将为 run automatically by Heroku。 build
将创建一个新的构建目录,clean
将转译代码并将其放置在 build-babel
目录中。完成后,Heroku 将修剪 build
,但您将不再需要 Babel。
当 Heroku 调用下面的 devDependencies
时,它将运行您转译的代码。您应该不会再看到 start
。
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"
},
脚本中调用 build
。如果这样做,devDependencies 将被修剪,您将获得以下内容
start