How do I specify a local version of Node for a project?
Solution 1
I believe that the engines and engineStrict are for when the package is being installed (via npm), not when you're trying to execute something with node. These options warn/prevent users from installing a package that is not designed to work (or compatible) with the node version they are currently using.
Solution 2
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."
Solution 3
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.
Solution 4
I have two solutions for this problem ....
Soln #1: Use a node version manager that can download and install Node and NPM for a specific version (and x86/x64 architecture for Windows) and then allow developers to switch versions.
Windows:
Mac/Linux:
Soln #2: Use a Docker image to run dev code on a Linux VM with your selected Node version. Your developers now all get an identical development environment that will hopefully match your final deployment environment.
This example shows you how to Dockerize your web app for deployment. During development you want to replace the COPY . /src
command use a volume to mounts code from your host filesystem to avoid doing image rebuilds as you update code. A trick is to create your base images and then derive development (./src is a volume) and deployment (copies ./src) images.
Finally you can also exploit Docker to do your CI testing
Refs:
sent1nel
JavaScript, Ruby, and Java. The Web, Android, and Linux. SOreadytohelp
Updated on February 15, 2022Comments
-
sent1nel over 2 years
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-definednpm 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?!