Node.js: 在Heroku上部署Node.js应用失败

3

我正在尝试将我的node.js应用程序部署到Heroku。 我已经做了必要的一切,但是我遇到了以下错误。

remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/nodejs
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  16.3.0
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 16.3.0...
remote:        Downloading and installing node 16.3.0...
remote:        Using default npm version: 7.15.1
remote:
remote: -----> Installing dependencies
remote:        Installing node modules
remote:        npm ERR! code E404
remote:        npm ERR! 404 Not Found - GET https://registry.npmjs.org/har-validator/-/har-validator-5.1.2.tgz - Not found
remote:        npm ERR! 404
remote:        npm ERR! 404  'har-validator@https://registry.npmjs.org/har-validator/-/har-validator-5.1.2.tgz' is not in the npm registry.
remote:        npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
remote:        npm ERR! 404
remote:        npm ERR! 404 Note that you can also install from a
remote:        npm ERR! 404 tarball, folder, http url, or git url.
remote:
remote:        npm ERR! A complete log of this run can be found in:
remote:        npm ERR!     /tmp/npmcache.7maaf/_logs/2021-06-13T18_35_23_460Z-debug.log
remote:
remote: -----> Build failed
remote:
remote:        We're sorry this build is failing! You can troubleshoot common issues here:
remote:        https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote:        If you're stuck, please submit a ticket so we can help:
remote:        https://help.heroku.com/
remote:
remote:        Love,
remote:        Heroku
remote:
remote:  !     Push rejected, failed to compile Node.js app.
remote:
remote:  !     Push failed
remote:  !
remote:  ! ## Warning - The same version of this code has already been built: 944efd5ebf13ff49831d29f02a9a1f6565355373
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 944efd5ebf13ff49831d29f02a9a1f6565355373
remote:  ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname>:main
remote:  !
remote:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote:
remote: Verifying deploy...
remote:
remote: !       Push rejected to stark-lowlands-47617.
remote:
To https://git.heroku.com/stark-lowlands-47617.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/stark-lowlands-47617.git'

我尝试做这件事:

git push heroku master:main

这是我的 package.json 文件:

{
  "name": "languagelearningapp",
  "engines" : { "node" : "16.3.0" },
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "basic-auth": "^2.0.1",
    "bcrypt": "^3.0.2",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "callback": "0.0.1",
    "express": "^4.16.4",
    "fcm-node": "^1.3.0",
    "fcm-notification": "^2.0.0",
    "firebase-admin": "^6.2.0",
    "jsonwebtoken": "^8.3.0",
    "mysql": "^2.16.0",
    "nodemon": "^1.18.5",
    "sendotp": "^1.2.9"
  }
}

但这并没有帮助。我还尝试将Node模块添加到.gitignore中,但它没有起作用。特别是,我尝试切换到不同的分支,但效果不好。

请帮助我。


看一下这个错误。它说 npm ERR! 404 'har-validator@https://registry.npmjs.org/har-validator/-/har-validator-5.1.2.tgz' 不在 npm 注册表中。如果你在 npm 上查找 har-validator` 这里,你会发现这个包已经被弃用了。 - Samathingamajig
请看这里,这是一个在GitHub上提出的问题,其中也涉及到har-validator被弃用的问题。其中一种解决方案建议首先升级HLS插件以消除对请求的依赖,从而避免引入har-validator,但需要注意他们使用了ArtilleryIO - Alex
你在本地开发机器上使用的 Node 版本是多少? - WaLid LamRaoui
@ WaLid LamRaoui,16.3.0 - testivanivan
1个回答

1
我认为您需要在本地机器上将 node 版本降级LTS版本,并使用 nvm 进行基本的重新配置,然后重置项目以使用 LTS 版本:删除 package.jsonpackage-lock.json,删除 node_modules 文件夹,然后运行 npm init 并安装您正在使用的依赖项,例如 bcrypt 等。

之后,您需要编辑新的 package.json 并告诉 Heroku 也使用 LTS 版本,方法是添加以下内容:

"engines": {
    "node": "14.x" 
  },

您的package.json应该类似于这样:
{
  "name": "languagelearningapp",
  "engines" : { "node" : "14.x" },
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    // your project dependencies 
  }
}

我知道以后可能会回来,但如果我遇到同样的问题,我会首先尝试这个。

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