将Node.js应用部署到Amazon Elastic Beanstalk时出错。

8

我最近在Beanstalk上部署了一个新的node.js应用程序,但始终遇到以下错误?

[实例:i-cce89e4a] 命令在实例上失败。返回代码:1 输出:(TRUNCATED)...opt/elasticbeanstalk/containerfiles/ebnode.py”,第180行,npm_install raise e subprocess.CalledProcessError:Command '['/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm','--production','rebuild']' 返回非零退出状态254。Hook /opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh 失败。要获取更多详细信息,请使用控制台或EB CLI检查/var/log/eb-activity.log。

我的node版本是:4.4.7

部署似乎在以下的最后一行失败:

 except Exception, e:
            npm_debug_log = '/var/log/nodejs/npm-debug.log'
            app_npm_debug_log = os.path.join(app_path, "npm-debug.log")
            write_event('Failed to run npm install. Snapshot logs for more details.', 'ERROR')
            if not os.path.exists(app_npm_debug_log):
                utc_time_str = datetime.datetime.utcnow().strftime("UTC %Y/%m/%d %H:%M:%S")
                err_msg = str.format("{0} cannot find application npm debug log at {1} \n", utc_time_str, app_npm_debug_log)
                print(err_msg)
                with open(npm_debug_log, 'a') as f:
                    f.write(err_msg)
            else:
                shutil.copyfile(app_npm_debug_log, npm_debug_log)
            raise e

我似乎找不到任何可靠的解决方案?

我手动压缩并上传了文件。

该应用程序在本地完美运行。

有人可以帮忙吗?

Application deployment failed at 2016-07-09T18:47:38Z with exit status 1 and error: Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh failed.

+ /opt/elasticbeanstalk/containerfiles/ebnode.py --action npm-install
npm WARN package.json dashboard@0.0.2 No repository field.

> history@1.17.0 postinstall /tmp/deployment/application/node_modules/history
> node ./npm-scripts/postinstall.js

npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "--production" "rebuild"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! path /tmp/deployment/application/node_modules/gulp-main-bower-files/node_modules/main-bower-files/node_modules/vinyl-fs/node_modules/duplexify/node_modules/readable-stream/node_modules/unreachable-branch-transform/node_modules/recast/node_modules/esprima/bin/esvalidate.js
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall chmod

npm ERR! enoent ENOENT: no such file or directory, chmod '/tmp/deployment/application/node_modules/gulp-main-bower-files/node_modules/main-bower-files/node_modules/vinyl-fs/node_modules/duplexify/node_modules/readable-stream/node_modules/unreachable-branch-transform/node_modules/recast/node_modules/esprima/bin/esvalidate.js'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! Please include the following file with any support request:
npm ERR! /tmp/deployment/application/npm-debug.log
Running npm install: /opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm
Setting npm config jobs to 1
npm config jobs set to 1
Running npm with --production flag
Failed to run npm install. Snapshot logs for more details.
Traceback (most recent call last):
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 695, in 
main()
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 677, in main
node_version_manager.run_npm_install(options.app_path)
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 136, in run_npm_install
self.npm_install(bin_path, self.config_manager.get_container_config('app_staging_dir'))
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 180, in npm_install
raise e
subprocess.CalledProcessError: Command '['/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm', '--production', 'rebuild']' returned non-zero exit status 254.
6个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
6

经过数小时的搜索,我可以说以下任一点都可能解决问题。

  • 在elasticbeanstalk配置中更改nodejs版本。
  • 在elasticbeanstalk配置中将实例类型从t2.micro更改为t2.small
  • 在elasticbeanstalk配置中设置elasticbeanstalk启动node应用程序时要运行的新命令(根据您的要求)。
  • 或在项目根目录创建一个名为.npmrc的文件,内容如下。
# Force npm to run node-gyp also as root, preventing permission denied errors in AWS with npm@5
unsafe-perm=true

2
这对我有用!npmrc文件修复了我的部署。 - Joel Denning

6

我没有足够的信息来回答这个问题... /var/log/eb-activity.log 说了什么?

但是,当在t2.small的beanstalk上运行npm install时,我遇到了类似的错误:它没有足够的RAM,npm失败了。升级到更大的实例可以解决这个问题。(提交node_module也可以解决这个问题,但我不想这样做)。

你应该连接到实例并使用top/htop观察部署时发生了什么。


1
我升级到了中型实例。 - user1526912
实际上我说得太早了 - 仍然遇到相同的错误。 - user1526912
遇到了相同的问题,升级到更高内存的实例后解决了。 - aravindaM
为了防止这种情况,我现在正在使用基于 Docker 的 Beanstalk,在 CircleCI 中将 node_modules 打包到 Docker 中。 - Boris Charpentier

4

我之前在压缩文件时包含了 node_modules 文件夹。排除掉这个文件夹后问题得到解决。


3
如果在 nano 实例 (例如 t3.nano) 上运行 npm install 失败,另一个解决方法是通过在项目的.ebextensions/swap.config中创建文件并添加以下内容以为实例启用交换内存:
commands:
    enable_swap_memory:
        command: "fallocate -l 1G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile"
        ignoreErrors: true

然后再次尝试运行eb deploy命令,这时npm install应该会成功。


0

在遇到持续的npm安装问题后,我决定跳过Elastic Beanstalk上的此步骤,而是将我的node_modules目录与来自我的CI环境(Github Actions)的构建文件一起上传。

我使用了这个模块来安装正确的Elastic Beanstalk配置。自述文件对问题进行了很好的解释: https://github.com/mixmaxhq/eb-disable-npm

我不得不将!node_modules添加到我的.ebignore中以确保它被推送。到目前为止,没有EB错误,部署速度更快。


0

为代码管道启用S3存储桶版本控制解决了我的问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,