NodeJS 我的代码在部署到Heroku时无法工作

68de4m5k  于 2022-11-03  发布在  Node.js
关注(0)|答案(3)|浏览(163)

当我在本地测试时,我的服务器运行良好。但是,将其部署到heroku时显示以下错误:

moh@ start /app
2017-05-01T08:04:40.867324+00:00 app[web.1]: > node server.js
2017-05-01T08:04:40.867324+00:00 app[web.1]: 
2017-05-01T08:04:41.360564+00:00 app[web.1]: module.js:597
2017-05-01T08:04:41.360579+00:00 app[web.1]:   return process.dlopen(module, path._makeLong(filename));
2017-05-01T08:04:41.360580+00:00 app[web.1]:                  ^
2017-05-01T08:04:41.360581+00:00 app[web.1]: 
2017-05-01T08:04:41.360582+00:00 app[web.1]: Error: /app/node_modules/bcrypt/lib/binding/bcrypt_lib.node: invalid ELF header
2017-05-01T08:04:41.360582+00:00 app[web.1]:     at Error (native)
2017-05-01T08:04:41.360583+00:00 app[web.1]:     at Object.Module._extensions..node (module.js:597:18)
2017-05-01T08:04:41.360584+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-01T08:04:41.360585+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-01T08:04:41.360585+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-05-01T08:04:41.360586+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-05-01T08:04:41.360586+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-05-01T08:04:41.360587+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/bcrypt/bcrypt.js:6:16)
2017-05-01T08:04:41.360588+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-05-01T08:04:41.360588+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-05-01T08:04:41.360589+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-01T08:04:41.360590+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-01T08:04:41.360590+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-05-01T08:04:41.360591+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-05-01T08:04:41.360592+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-05-01T08:04:41.360592+00:00 app[web.1]:     at Object.<anonymous> (/app/usersDetailsRoutes.js:3:14)
2017-05-01T08:04:41.370305+00:00 app[web.1]: 
2017-05-01T08:04:41.378189+00:00 app[web.1]: npm ERR! Linux 3.13.0-112-generic
2017-05-01T08:04:41.378466+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-05-01T08:04:41.378704+00:00 app[web.1]: npm ERR! node v6.10.2
2017-05-01T08:04:41.378844+00:00 app[web.1]: npm ERR! npm  v3.10.10
2017-05-01T08:04:41.378970+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-05-01T08:04:41.379105+00:00 app[web.1]: npm ERR! moh@ start: `node server.js`
2017-05-01T08:04:41.379189+00:00 app[web.1]: npm ERR! Exit status 1
2017-05-01T08:04:41.379285+00:00 app[web.1]: npm ERR! 
2017-05-01T08:04:41.379382+00:00 app[web.1]: npm ERR! Failed at the moh@ start script 'node server.js'.
2017-05-01T08:04:41.379537+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
2017-05-01T08:04:41.379634+00:00 app[web.1]: npm ERR! If you do, this is most likely a problem with the moh package,
2017-05-01T08:04:41.379743+00:00 app[web.1]: npm ERR! not with npm itself.
2017-05-01T08:04:41.379844+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-05-01T08:04:41.379931+00:00 app[web.1]: npm ERR!     node server.js
2017-05-01T08:04:41.380018+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-05-01T08:04:41.380103+00:00 app[web.1]: npm ERR!     npm bugs moh
2017-05-01T08:04:41.380233+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-05-01T08:04:41.380389+00:00 app[web.1]: npm ERR!     npm owner ls moh
2017-05-01T08:04:41.380459+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-05-01T08:04:41.384059+00:00 app[web.1]: 
2017-05-01T08:04:41.384227+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-05-01T08:04:41.384288+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2017-05-01T08:04:41.507958+00:00 heroku[web.1]: State changed from starting to crashed
2017-05-01T08:04:41.509045+00:00 heroku[web.1]: State changed from crashed to starting
2017-05-01T08:04:41.492959+00:00 heroku[web.1]: Process exited with status 1
2017-05-01T08:04:43.465939+00:00 heroku[web.1]: Starting process with command `npm start`
2017-05-01T08:04:45.782290+00:00 app[web.1]: 
2017-05-01T08:04:45.782305+00:00 app[web.1]: > moh@ start /app
2017-05-01T08:04:45.782306+00:00 app[web.1]: > node server.js
2017-05-01T08:04:45.782307+00:00 app[web.1]: 
2017-05-01T08:04:46.242097+00:00 app[web.1]: module.js:597
2017-05-01T08:04:46.242120+00:00 app[web.1]:   return process.dlopen(module, path._makeLong(filename));
2017-05-01T08:04:46.242121+00:00 app[web.1]:                  ^
2017-05-01T08:04:46.242122+00:00 app[web.1]: 
2017-05-01T08:04:46.242123+00:00 app[web.1]: Error: /app/node_modules/bcrypt/lib/binding/bcrypt_lib.node: invalid ELF header
2017-05-01T08:04:46.242123+00:00 app[web.1]:     at Error (native)
2017-05-01T08:04:46.242124+00:00 app[web.1]:     at Object.Module._extensions..node (module.js:597:18)
2017-05-01T08:04:46.242125+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-01T08:04:46.242125+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-01T08:04:46.242126+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-05-01T08:04:46.242127+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-05-01T08:04:46.242127+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-05-01T08:04:46.242128+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/bcrypt/bcrypt.js:6:16)
2017-05-01T08:04:46.242128+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-05-01T08:04:46.242129+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-05-01T08:04:46.242130+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-01T08:04:46.242130+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-01T08:04:46.242131+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-05-01T08:04:46.242132+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-05-01T08:04:46.242132+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-05-01T08:04:46.242133+00:00 app[web.1]:     at Object.<anonymous> (/app/usersDetailsRoutes.js:3:14)
2017-05-01T08:04:46.250340+00:00 app[web.1]: 
2017-05-01T08:04:46.257941+00:00 app[web.1]: npm ERR! Linux 3.13.0-112-generic
2017-05-01T08:04:46.258139+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-05-01T08:04:46.258285+00:00 app[web.1]: npm ERR! node v6.10.2
2017-05-01T08:04:46.258630+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-05-01T08:04:46.258450+00:00 app[web.1]: npm ERR! npm  v3.10.10
2017-05-01T08:04:46.258743+00:00 app[web.1]: npm ERR! moh@ start: `node server.js`
2017-05-01T08:04:46.258836+00:00 app[web.1]: npm ERR! Exit status 1
2017-05-01T08:04:46.258984+00:00 app[web.1]: npm ERR! 
2017-05-01T08:04:46.259080+00:00 app[web.1]: npm ERR! Failed at the moh@ start script 'node server.js'.
2017-05-01T08:04:46.259251+00:00 app[web.1]: npm ERR! If you do, this is most likely a problem with the moh package,
2017-05-01T08:04:46.259162+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
2017-05-01T08:04:46.259342+00:00 app[web.1]: npm ERR! not with npm itself.
2017-05-01T08:04:46.259435+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-05-01T08:04:46.259520+00:00 app[web.1]: npm ERR!     node server.js
2017-05-01T08:04:46.259593+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-05-01T08:04:46.259672+00:00 app[web.1]: npm ERR!     npm bugs moh
2017-05-01T08:04:46.259762+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-05-01T08:04:46.259842+00:00 app[web.1]: npm ERR!     npm owner ls moh
2017-05-01T08:04:46.259934+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-05-01T08:04:46.263264+00:00 app[web.1]: 
2017-05-01T08:04:46.263441+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-05-01T08:04:46.263496+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2017-05-01T08:04:46.321834+00:00 heroku[web.1]: Process exited with status 1
2017-05-01T08:04:46.332504+00:00 heroku[web.1]: State changed from starting to crashed

我尝试在package.json文件中添加以下内容

"scripts": {
        "start": "node server.js"
    }

但我最终还是犯了同样的错误
下面是我的server.js代码:

var express    = require('express');
var app        = express();
var bodyParser = require('body-parser');
var routes     = require('./routes');
var usersRoute = require('./usersDetailsRoutes');

var morgan = require('morgan');  

var mongoose = require("mongoose");
mongoose.connect('mongodb://behrouz:zxcv1234@ds017514.mlab.com:17514/sourceplaze');

require('./config/passport')(passport);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(morgan('dev'));

app.use(passport.initialize());

var port = process.env.PORT || 8080;

app.use('/', routes);
app.use('/users', usersRoute)

app.listen(port);
console.log('listening on port: ' + port);

module.exports = app;

我做错了什么?

jutyujz0

jutyujz01#

我的猜测是,您使用一个在Heroku上不起作用的 bcrypt 包部署了node-modules项目文件夹的内容。
错误:/app/node_modules/bcrypt/lib/绑定/bcrypt_lib.节点:ELF标头无效
您应该在.gitignore中添加node-modules文件夹,并按照this answer的建议在服务器上部署package.json文件。

ygya80vv

ygya80vv2#

我知道这是一个老职位,但只是找到了一个选项,为我工作

  1. npm卸载bcrypt
  2. npm安装bcryptjs
    在代码中出现bcrypt的所有位置将其替换为bcryptjs,或者仅将const bycrypt = require('bcryptjs')替换为const bcrypt = require('bcrypt')
a5g8bdjr

a5g8bdjr3#

npm卸载bcrypt npm安装bcryptjs在代码中出现bcrypt的地方用bcryptjs替换bcrypt,或者只使用const bycrypt = require('bcryptjs ')而不是const bcrypt = require('bcrypt')

相关问题