使用 ZIP 将 nodejs 应用程序部署到 Elastic BeanStalk => 找不到本地包
Posted
技术标签:
【中文标题】使用 ZIP 将 nodejs 应用程序部署到 Elastic BeanStalk => 找不到本地包【英文标题】:nodejs app deployment with ZIP to Elastic BeanStalk => can't find local package 【发布时间】:2020-09-02 02:02:01 【问题描述】:在 Elastic Beanstalk 上,我想使用 zip 功能而不是使用命令 eb deploy
的 EB CLI 上传我的应用程序
不幸的是,我收到以下错误,服务器在子目录./src/server/
中找不到本地模块。
提前感谢您的建议
> nodejs@0.0.1 start /var/app/current
> node www
internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module './src/server/app'
Require stack:
1. /var/app/current/www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/var/app/current/www:3:13)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
code: 'MODULE_NOT_FOUND',
requireStack: [ '/var/app/current/www' ]
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodejs@0.0.1 start: `node www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodejs@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
我做了什么实验
-
用我的代码测试
eb deploy
=>成功
压缩我的代码并将其加载到 Elastic Beanstalk => 失败
比较从第 1 步和第 2 步创建的 zip => 找不到任何差异
代码在我的本地机器上始终运行良好!
package.json
"name": "nodejs",
"version": "0.0.1",
"description": "- [ExpressJS](https://expressjs.com/) ",
"main": "gulp.js",
"scripts":
"start": "node www"
,
"author": "someone",
"license": "ISC",
"dependencies":
"express": "^4.17.1"
www
#!/usr/bin/env node
var app = require('./src/server/app');
var debug = require('debug')('testeb:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val)
var port = parseInt(val, 10);
if (isNaN(port))
// named pipe
return val;
if (port >= 0)
// port number
return port;
return false;
function onError(error)
if (error.syscall !== 'listen')
throw error;
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code)
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
function onListening()
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
src/server/app.js
var express=require("express")
const app=express()
app.get('/test',(req,res)=>
let t=new Date() +':'+ req.path
console.log(t)
res.send("test OK : " + t ))
module.exports=
app
【问题讨论】:
【参考方案1】:无需深入研究问题,试试这个
zip app.zip -r * .[^.]* -x *.git* *.env*
我也遇到了同样的问题,但是成功了
【讨论】:
以上是关于使用 ZIP 将 nodejs 应用程序部署到 Elastic BeanStalk => 找不到本地包的主要内容,如果未能解决你的问题,请参考以下文章
上传/部署 zip 文件时,不会创建 .platform 挂钩中的 AWS Elastic Beanstalk 自定义 nginx .conf 文件