I followed the basic getting started instructions for node.js on Heroku here:
https://devcenter.heroku.com/categories/nodejs
These instruction don't tell you to create a .gitignore node_modules, and therefore imply that node_modules should be checked in to git. When I include node_modules in git my getting started application ran correctly.
When I followed the more advanced example at:
https://devcenter.heroku.com/articles/realtime-polyglot-app-node-ruby-mongodb-socketio https://github.com/mongolab/tractorpush-server (source)
It instructed me to add node_modules to .gitignore. So I removed node_modules from git, added it to .gitignore, then re-deployed. This time the deployed failed like so:
-----> Heroku receiving push
-----> Node.js app detected
-----> Resolving engine versions
Using Node.js version: 0.8.2
Using npm version: 1.0.106
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
Error: npm doesn't work with node v0.8.2
Required: node@0.4 || 0.5 || 0.6
at /tmp/node-npm-5iGk/bin/npm-cli.js:57:23
at Object.<anonymous> (/tmp/node-npm-5iGk/bin/npm-cli.js:77:3)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)
at Module._compile (module.js:449:26)
Error: npm doesn't work with node v0.8.2
Required: node@0.4 || 0.5 || 0.6
at /tmp/node-npm-5iGk/bin/npm-cli.js:57:23
at Object.<anonymous> (/tmp/node-npm-5iGk/bin/npm-cli.js:77:3)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)
at Module._compile (module.js:449:26)
Dependencies installed
-----> Discovering process types
Procfile declares types -> mongod, redis, web
-----> Compiled slug size is 5.0MB
-----> Launching... done, v9
Running "heroku ps" confirms the crash. Ok, no problem, so I rolled back the change, add node_module back to the git repository and removed it from .gitignore. However, even after reverting, I still get the same error message on deploy but now the application is running correctly again. Running "heroku ps" tells me the application is running.
So my question is what's the right way to do this? Include node_modules or not? And why would I still be getting the error message when I rollback? My guess is the git repository is in a bad state on the Heroku side?
What worked for me was explicitly adding a npm version to package.json ("npm": "1.1.x") and NOT checking in node_modules to git. It may be slower to deploy (since it downloads the packages each time), but I couldn't get the packages to compile when they were checked in. Heroku was looking for files that only existed on my local box.
http://nodejs.org/api/modules.html
If you're rolling your own modules specific to your app, you can keep those (and only those) in your app's
/node_modules
. And move out all the other dependencies to the parent directory.This use case of pretty awesome, it lets you keep modules you created specifically for your app nicely with your app, and doesn't clutter your app with dependencies which can be installed later.
I was going to leave this after this comment: Should I check in node_modules to git when creating a node.js app on Heroku?
But stackoverflow was formatting it weird. If you don't have identical machines and are checking in node_modules, do a .gitignore on the native extensions. Our .gitignore looks like:
Test this by first checking everything in, and then have another dev do the following:
Ensure that no files changed.
Instead of checking in node_modules, make a package.json file for your app.
The package.json file specifies the dependencies of your application. Heroku can then tell npm to install all of those dependencies. The tutorial you linked to contains a section on package.json files.
I believe that
npm install
should not run in a production environment. There are several things that can go wrong - npm outage, download of newer dependencies (shrinkwrap seems to solved this) are two of them.On the other hand,
node_modules
should not be committed on git. Apart from their big size, commits including them can become distracting.The best solutions would be this:
npm install
should run in a CI environment that is similar to the production environment. All tests will run and a zipped release file will be created that will include all dependencies.You should not include
node_modules
in your.gitignore
(or rather you should includenode_modules
in your source deployed to Heroku).If
node_modules
:npm install
will use those vendored libs and will rebuild any binary dependencies withnpm rebuild
.npm install
will have to fetch all dependencies itself which adds time to the slug compile step.See the Node.js buildpack source for these exact steps
However, the original error looks to be an incompatibility between the versions of
npm
andnode
. It is a good idea to always explicitly set theengines
section of yourpackages.json
according to this guide to avoid these types of situations:This will ensure dev/prod parity and reduce the likelihood of such situations in the future.