0

I know it is possible to switch between different versions of Node using NVM, n, or similar.

Is there a convenient way for the right version of Node to automatically be used when running commands within a given package? ("Right version" being determined by the engine tag or similar).

For instance, I would like to be able to do this:

cd mypackage-that-needs-node10
npm run serve
# ... node 10 is used
cd ..
cd mypackage-that-needs-node14
npm run serve
# ... node 14 is used
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • What does "running commands within a given package" mean? You run exactly ONE nodejs version for your app and ALL the packages that it uses. So, you select packages for your app that can be run on a common version of nodejs and you configure your app to run that specific nodejs version. Individual API calls into different modules within your app cannot use different versions of nodejs. All modules loaded into your app use the same version of nodejs. Please provide specific examples of what problem you have. – jfriend00 Aug 08 '21 at 08:11
  • Added some clarification. By "within a given package", I mean "within a given app" in your terminology. (I mean, in Node, every app is also a package...) – Steve Bennett Aug 08 '21 at 11:15

2 Answers2

2

n supports an engine label to find Node.js version from package.json, and auto which includes more sources such as a .node-version file.

For example:

$ n install engine
# or run the target version of node
$ n run auto index.js
# or execute a command with that Node.js in path
$ n exec auto npm run serve
shadowspawn
  • 3,039
  • 22
  • 26
0

A possible approach is to install node itself into your package, and have npm run scripts use it in preference to the system version of node. See node

shadowspawn
  • 3,039
  • 22
  • 26