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 it seems like it's not able to read the node and npm versions. 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?
#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
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
I added the version to the package.json as others mentioned and it worked for me. These versions were referenced from the defaults that Heroku uses during the build if none are specified:
"engines": {
"node": "14.18.3",
"npm": "6.14.15"
},
Also, I'd recommend adding this setting to your app via the CLI for more output, which really helps:
heroku config:set NPM_CONFIG_LOGLEVEL=verbose --app=<your_app_name>
👍🏼
In an easy way
1- check your node version, let say it is like this: "node": "17.3.0"
2- Go inside the the package.json, at the very top below name and version add this
"engines": {
"node": "17.3.0",}
3- Delete package-lock.json then reinstall it by npm install.
After 3 hours working this mechanism helped me, I wanted to share with you all :) I hope it works for you too
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
Don't give 'x' on the version
"node": "0.12.x",
"npm": "2.5.x"
write complete version.
Ex.
"node": "0.12.0",
"npm": "2.5.0"
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
Make sure the lockfile is up to date
In order to update the package-lock.json, run npm install and check in the changes to the file to git.
Then, git add . && git commit -m "update pkg-lock" && git push heroku master
I added the node version to package.json, but it would only accept it in the format
<major version>.x
i.e.
"name": "myapp",
"version": "1.0.0",
"engine": {
"node": "16.x"
}
I got that info from the heroku docs here - no other way of specifying it worked except that 16.x.
In my case the issue was with package.json, it was missed up due to some manual changes, so reverting to old version of repo solved my issue.
Related
Hello I downloaded a react project from https://github.com/fabiau/jc-calendar and when I do npm start I get error messages. I tried to do "NPM Fund" and "NPM Update" and none of them worked. Obviously i have no clue what I am doing so if anyone can point me in the right direction I would apprecaite it.
Before starting dev server you'll have to install the dependencies of the project using npm install , as generally node_modules/dependencies are not part of the repository.
This is mentioned in Readme.md of JC-calender.
Try deleting folder node_modules and package-lock.json if exists and run command npm install
Checkout scripts in package.json in usual case start will be as given below but as per the code you consider npm-run-all package is used so be sure to run npm install
"scripts": {
"start": "react-scripts start",
},
This may help you out https://www.npmjs.com/package/npm-run-all
most of the repositories on GitHub, don't have dependencies installed, after downloading a repository on your local machine, you need to run "npm install". The reason behind this is that npm will look for all the required dependencies of the specified project and install them on your machine, with node-modules, then run "npm start"
I have some strange issue regarding the package-lock.json.
We are using npm 5.6 and node 8.9.2.
We have some project with dependencies. The developers can install the deps and push the package-lock.json as recommended:
...
"dependencies": {
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM=",
"dev": true
}
}
...
They are getting their dependencies from https://registry.npmjs.org/..
Now we have some jenkins which gets its dependencies from some artifactory which serves as 'proxy' to https://registry.npmjs.org.
The build fails 75% of the times on some error like:
npm ERR! Unexpected end of JSON input while parsing near '...nalInternal" : {
Even when I clean the cache before with sudo npm clean cache --force (or remove .npm)
The error occurs always on a different place.
Now when I remove the package-lock.json before starting the build it works well every time.
A new package-lock.json is generated. It contains not the registry urls but the artifactory urls (https://artifactory/xxx). I thought it could maybe have to do with it. so I replaced the current package-lock.json in git with the one from jenkins. I tried again but again the same error and issue, only deleting the package-lock.json seems to solve it but as I read it's not recommended to ignore this file.
What could be an issue?
In a project I inherited, the packages.json looks roughly like this:
{
"name": "...",
"version": "...",
"description": "",
"author": "...",
"license": "ISC",
"dependencies": {
"lodash": "^3.10.1",
"assemble": "^0.4.37",
"cheerio": "^0.16.0",
"grunt": "^0.4.4",
"grunt-build-control": "^0.1.3",
},
"keywords": [
"handlebars-helper-md",
"handlebars-helper-rel"
]
}
When I first got it, lodash was ~2.4.1 and I'm trying to update it to 3.10.1 (as shown above). However, npm continues to install 2.4.1 at the top-level (Despite the package.json requesting the newer version) and it does not install the requested versions 2.4.1 or 2.4.2 in some of the dependencies (like assemble and cheerio). Thus when I npm install lodash#3.10.1 it complains about unmet dependencies.
I've tried removing node_modules and npm clear cache and rm -rf $HOME/.npm in different orders and combinations with no change.
How do I get lodash#3.10.1 to install at the top-level and the requested version of lodash in all the dependencies (and not have the dependencies use the top-level copy of lodash -- which I thought was the normal way for npm to work)?
Preferably a solution would not require updating all the dependencies to new versions (assuming that is even possible). That could be a solution, but that would require a lot more validation to make sure nothing broke.
I'v made a static single page site using grunt. I'm now trying to deploy it to heroku using the heroku-buildpack-nodejs-grunt for node grunt.
Below is a pic of my root directory:
Here's my Gruntfile package.json:
Procfile:
web: node index.html
When I run $ git push heroku master it gets to the Gruntfile and fails:
-----> Found Gruntfile, running grunt heroku:production task
>> Local Npm module "grunt-contrib-uglify" not found. Is it installed?
The above errors proceed to list all local NPM modules as not found. If I list all loadNpmTasks instead of using "load-grunt-tasks", I get the exact same error.
When I $ heroku logs I get:
Starting process with command `node web.js`
Error: Cannot find module '/app/web.js'
Can anyone see where I've gone wrong?
For anyone passing by here, I wasn't able to solve the problem. This is where I got to:
In my Gruntfile, I moved npm modules from devDependencies to dependencies. Heroku was then able to install these dependencies.
However, when Heroku ran the tasks, it stops at the haml task w/ error "You need to have Ruby and Haml installed and in your PATH for this task to work". Adding ruby & haml to the Gruntfile as engines did not work.
The only thing I can think of is that maybe Heroku installs your devDependencies first, tries to run Grunt, but since it didn't install load-grunt-tasks yet, you don't get the grunt.loadNpmTasks( 'grunt-contrib-uglify' ); line (which load-grunt-tasks does for you), and thus Grunt can't find the package.
Can you try changing your Gruntfile to explicitly list out all npm modules using the grunt.loadNpmTasks() method?
EDIT:
Just remembered another thing I had to do:
heroku labs:enable user-env-compile -a myapp
heroku config:set NODE_ENV=production
(Obviously replacing myapp with your Heroku app name.)
This makes Heroku allow user set environment variables and then sets your server to production. Try that, and set your dependencies and devDependencies as you had them originally (just to see if it works).
I am coming pretty late to the game here but I have used a couple methods and thought I would share.
Option 1: Get Heroku to Build
This is not my favorite method because it can take a long time but here it is anyway.
Heroku runs npm install --production when it receives your pushed changes. This only installs the production dependencies.
You don't have to change your environment variables to install your dev dependencies. npm install has a --dev switch to allow you to do that.
npm install --dev
Heroku provides an article on how you can customize your build. Essentially, you can run the above command as a postinstall script in your package.json.
"scripts": {
"start": "node index.js",
"postinstall": "npm install --dev && grunt build"
}
I think this is cleaner than putting dev dependencies in my production section or changing the environment variables back and forth to get my dependencies to build.
Also, I don't use a Procfile. Heroku can run your application by calling npm start (at least it can now almost two years after the OP). So as long as you provide that script (as seen above) Heroku should be able to start your app.
As far as your ruby dependency, I haven't attempted to install a ruby gem in my node apps on Heroku but this SO answer suggests that you use multi buildpack.
Option 2: Deploy Your Dependencies
Some argue that having Heroku build your application is bad form. They suggest that you should push up all of your dependencies. If you are like me and hate the idea of checking in your node_modules directory then you could create a new branch where you force add the node_modules directory and then deploy that branch. In git this looks like:
git checkout -b deploy
git add -f node_modules/
git commit -m "heroku deploy"
git push heroku --force deploy:master
git checkout master
git branch -D deploy
You could obviously make this into a script so that you don't have to type that every time.
Option 3: Do It All Yourself
This is my new favorite way to deploy. Heroku has added support for slug deploys. The previous link is a good read and I highly recommend it. I do this in my automated build from Travis-CI. I have some custom scripts to tar my app and push the slug to Heroku and its fast.
I faced a similar problem with Heroku not installing all of my dependencies, while I had no issue locally. I fixed it by running
heroku config:set USE_NPM_INSTALL=true
into the path, where I deployed my project from. This instructs Heroku to install your dependencies using npm install instead of npm ci, which is the default! From Heroku dev center:
"Heroku uses the lockfiles, either the package-lock.json or yarn.lock, to install the expected dependency tree, so be sure to check those files into git to ensure the same dependency versions across environments. If you are using npm, Heroku will use npm ci to set up the build environment."
Using the $ bower init command, I have created a bower.json for my package and registered it with Bower, no problem.
After looking at the Github homepages for some popular Bower packages, e.g. RequireJS and Modernizr, I've noticed their repo's don't contain a bower.json or a component.json. How is this possible?
I've also noticed that when I download any Bower package, the package contains a .bower.json file (note the dot in the beginning) and that file contains quite a bit more information than what I was asked during $ bower init for my package. For example, below is the .bower.json from Modernizr:
{
"name": "modernizr",
"homepage": "https://github.com/Modernizr/Modernizr",
"version": "2.6.2",
"_release": "2.6.2",
"_resolution": {
"type": "version",
"tag": "v2.6.2",
"commit": "ca45d02757b83367d8455d477e3cbc42dfdee035"
},
"_source": "git://github.com/Modernizr/Modernizr.git",
"_target": "~2.6.2",
"_direct": true
}
When I download my newly created package, it just contains the same info that I originally checked in to git.
Is there a new format for bower.json that I should be using? Or did I simply miss something in the setup process?
Bower doesn't need a bower.json or a component.json to install packages. The manifest file provide some useful info, like dependencies, ignored files, version and etc, but in the end it just downloads Git commits and place them somewhere.
In the case of Modernizr/Require.js, someone just registered their repos and Bower is retrieving a tag.
About the .bower.json file: this is generated by Bower itself after installing a package. It contains some more verbose info about a package, like the commit from which the package was retrieved, for example.
TL;DR: Keep using bower init, it'll do the right thing for you!
I am using Yeoman and the following is the content of my bower.json file. I thought it might help you. (I have installed all the latest versions of bower and grunt)
{
"name": "yowebapp",
"version": "0.0.0",
"dependencies": {
"sass-bootstrap": "~3.0",
"requirejs": "~2.1.4",
"modernizr": "~2.6.2",
"jquery": "~1.9.1",
"d3":"~3.3.2",
"angular":"1.0.7"
},
"devDependencies": {}
}
and I download my dependencies with bower install.