Different node version for different projects, is there a way of telling node which version to use?
Asked Answered
B

5

13

I have a pretty common (i guess) problem. Many of my projects utilize nodejs, some for business logic, others only for some building task.

I need to have different runtimes in different projects, one of my electron apps requires node 7.10.0, a typical build suite requires node 8.x.

Now i know - i can use sudo n 7.10.0 or sudo n latest to switch the runtime globally on my computer (For those, who dont know this - have a look at "n")

Anyway, IMO this is not so convenient (some times, i need to rebuild all the modules after switching versions, often i forget to switch and so on). Is there a way of telling node which interpreter to use? Can i use a .npmrc file in a project directory to force a specific nodejs version within that subdirectory?

I searched exactly for this (npmrc node version) but was not lucky enough to find something.

Bestraddle answered 22/11, 2017 at 12:14 Comment(0)
B
3

Okay, i found a similar quesion:

Automatically switch to correct version of Node based on project

it seems you can install "avn" and use a .node-version file to do exactly that.

sudo npm install -g avn avn-n
avn setup

then you can create a .node-version file in your project and enter the desired version

echo 7.10.0 > .node-version

Then avn will detect that and activate the correct version

Unfortunately i get an additional permissions error. So to make this work, you need to install/configure "n" to work without sudo/root.

Bestraddle answered 22/11, 2017 at 12:53 Comment(2)
hello, same permission error for me checkPermissions Missing write access, Error: EACCES: permission denied, access '/usr/local/lib/node_modules/avn. How did you solve that?Ras
Before installing npm create a file called .npmrc and place it inside your home directory. the content of that file: prefix=${HOME}/.npm-packages This will make npm install packages to your home directory rather than usr/local... If you dont want to reinstall everything you can try to fix all kinds of permissions manually. Have a look at the value of your $N_PREFIX environment variable. This is the location your packages are installed. I tried to fix that but ended up with reinstalling everything :-)Bestraddle
O
2

If you're fine with using another tool you could use nvshim.

pip install nvshim  # this is all you need to do

It does not slow your shell startup or switching directories, instead moving the lookup of which node version to when you call node, npm or npx by shimming those binaries. More details in the docs.

Source, I wrote the tool.

Orthodontics answered 21/9, 2020 at 2:25 Comment(0)
S
0

Volta can be used to manage multiple nodejs, npm or yarn versions on different projects on same machine. It's cross-platform.

For example you can run volta pin node@14 in project directory and this will set node to v14 if it exists otherwise it will download and then set it.

More information here https://docs.volta.sh/guide/

Saponaceous answered 22/12, 2022 at 13:34 Comment(0)
S
0

Exact the other version of node in a folder example C:\Program files\nodejs_v14_17_0

Open a PowerShell prompt that you intend to run project on

$Env:Path = "C:\Program files\nodejs_v14_17_0;" + $Env:PATH

This command prompt will now pick up the alternative nodejs (and npm) for this command prompt only

Then start your program:

npm start
Slab answered 17/3 at 15:40 Comment(0)
C
-1

NVM (Node Version Manager) allow us to use different versions of node quite easily on a single machine. You can have a look at here how to configure and use it.

Capone answered 18/10, 2021 at 17:37 Comment(1)
The link is misleading. It doesn't have any mention of how you used NVM in different projects. @Daud AhmedDamsel

© 2022 - 2024 — McMap. All rights reserved.