Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify a local version of Node for a project?

Tags:

I've got a project where we're trying to get Node up and running across multiple developers' machines. The problem is that not all of the developers are Node (or even JavaScript) developers, and we want to ensure that they have the Node version necessary to run a specific project (developers will have multiple Node projects on their machines).

I read about package.json's "engines" field, but I couldn't seem to find any way to get the version of Node installed that I needed. To test, I set my current node version to v0.10.29 via NVM, created a package.json specifying a necessary engine of v0.11.13, and tried to start Node via the node command as well as via a package.json-defined npm start command.

blackjack:node-engines-test sent1nel$ node -v
v0.10.29
blackjack:node-engines-test sent1nel$ cat package.json
{
  "name": "node-engines-test",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "engineStrict": true,
  "engines": {
    "node": "v0.11.13"
  },
  "start": "node index.js",
  "author": "",
  "license": "ISC"
}
blackjack:node-engines-test sent1nel$ cat index.js
console.log('Version: ' + process.version);
blackjack:node-engines-test sent1nel$ node index.js
Version: v0.10.29
blackjack:node-engines-test sent1nel$ npm start
blackjack:node-engines-test sent1nel$

npm install doesn't seem to care about the node engine version either.

blackjack:node-engines-test sent1nel$ npm install
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data
blackjack:node-engines-test sent1nel$ node -v
v0.10.29

What gives?!

like image 956
sent1nel Avatar asked Jul 21 '14 16:07

sent1nel


People also ask

How do I specify node version?

The n command for installing and activating a version of Node is simple: n 6.17. 1 . You could also use n latest for the latest version of Node or n lts for the latest LTS version of Node. If the version of Node is already installed, then n will simply switch to that version.

How do I find my local node version?

To see if Node is installed, open the Windows Command Prompt, Powershell or a similar command line tool, and type node -v . This should print the version number so you'll see something like this v0.


2 Answers

The npm registry includes a package called “node”. It is a regular npm package that contains only the Node.js binary.

So, in your directory in question run:

npm install [email protected] --save-exact

Then, put a script in your package.json as follows:

"scripts": {
  "v": "node -v"
}

To verify, run node -v in your terminal in the root of the project and you should see the version you have set on your machine. Compare that by running npm run v and you should see the version you have set for the project. This way, you can seamlessly move about your file system and execute various builds without changing your global node configuration.

In principle, every executable file that arrives with an npm package is linked to the local binaries directory within the project. It means that when we install such a package, we could find a link for its executable file inside.

Note: set node engine to advise - "this field is advisory only and will only produce warnings when your package is installed as a dependency."

like image 106
Luke Woodward Avatar answered Sep 23 '22 02:09

Luke Woodward


NVM + .nvmrc

If you are using NVM like this, which you likely should, then you can indicate the nodejs version required for given project in a git-tracked .nvmrc file:

node --version > .nvmrc

or:

echo v10.15.1 > .nvmrc

This does not take effect automatically on cd, which is sane: the user must then do a:

nvm use

and now that version of node will be used for the current shell.

You can list the versions of node that you have with:

nvm list

.nvmrc is documented at: https://github.com/creationix/nvm/tree/02997b0753f66c9790c6016ed022ed2072c22603#nvmrc

Tested with NVM 0.33.11.

Heroku does respect package.json engines:

Worth mentioning, as documented here, Heroku does play it nice and obey the engines: entry e.g.:

  "engines": {
    "node": "14.17.0",
    "npm": "6.14.13"
  },

So you should Always, Always set that to what you are using locally.