Heroku does not read node version

2019-04-05 04:49发布

问题:

I have a Node project that I want to host on Heroku. I have explicitly defined node and npm versions in my package.json (located in the root directory), which looks like this:

{
  "name": "*********",
  "version": "0.0.0",
  "private": true,
  "engines": {
    "node": "0.12.x",
    "npm": "2.5.x"
  },
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "^1.13.3",
    ...
}

However, when I try to push the app to heroku

git push heroku master 

Heroku tries to build the app, but seems not to be able to reed the node and npm version. Here is the response i get.

remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:         
remote:        Resolving node version (latest stable) via semver.io...
remote:        Downloading and installing node 4.2.1...
remote:        Using default npm version: 2.14.7

Why does heroku not read the node and npm version from package.json?

回答1:

@rdegges was right that the package.json wasn't correctly committed to Heroku. So just following the Heroku instructions didn't work for me for some reason. Here is what I had to do in order to make it work.

git clone <my git project>
heroku create <app name>

#remove package.json
mv package.json tmp_package.json
git add package.json
git commit -m "removed package.json"

#re-add package.json
mv tmp_package.json package.json
git add package.json
git commit -m "re-added package.json"

git push heroku master


回答2:

This works for me -- make sure you've actually committed these changes to Git, and pushed the repository to Heroku. You may also want to specify exact Node and NPM release numbers for your Heroku app.

While this WILL WORK with the variable releases you've got specified, it's not recommended, as small changes in releases might cause issues for you.

For reference, here are the Heroku docs on specifying a Node.js runtime as well: https://devcenter.heroku.com/articles/nodejs-support#node-js-runtimes



回答3:

Maybe your master branch is not the branch is not updated, try merging the branch that you want to deploy into master in order to use:

git push heroku master


回答4:

I tried the other solutions, but it didn't work for me. However, by changing the name field in package.json, it worked:

From:

{
  ...
  "name": "foo"
  ...
}

To:

{
  ...
  "name": "bar"
  ...
}

Alternative 2:

When I had to do the same on my other computer, it didn't work, but I tried removing package.json, recreating it from scratch, and then it worked for some odd reason (file metadata?):

$ rm package.json
$ npm init