Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

node.js

npm

n

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.

like image 393
Philipp Wrann Avatar asked Nov 22 '17 12:11

Philipp Wrann


1 Answers

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.

like image 99
Philipp Wrann Avatar answered Oct 12 '22 13:10

Philipp Wrann