shreyasm-dev
shreyasm-dev

Reputation: 2844

Set node version for a single command using nvm

I have a command that must be run with Node 16 installed, no other version. However, I need to have the latest version of Node installed for regular use.

How can I configure things, perhaps with environment variables, so that just that one command uses Node 16?

Something like nvm use 16 && node -v && nvm use 19 is too slow, but aliasing in .zshrc is an option.

Upvotes: 2

Views: 2378

Answers (2)

ARKhan
ARKhan

Reputation: 2015

I am not sure this answer your question. But I have too similar requirements where a Go program triggers a Node.js program using Golang os/exec package. That Node.js program must run on a specific version of node (in my case 14.17.0) and I was using nvm which has 17.5.0 set in my system.

So, I was able to switch version dynamically from 17.5.0 to 14.17.0 using environment variable set before running Go program.

export NVM_BIN=/Users/<username>/.nvm/versions/node/v14.17.0/bin
export PATH=$NVM_BIN:$PATH

# `echo $PATH` output should contains this path at the beginning for node i.e. /Users/<username>/.nvm/versions/node/v14.17.0/bin

Upvotes: 0

Nifess
Nifess

Reputation: 36

What I've done in one of my Projects is this:

I've switched to node 16: nvm use 16. After that which node showed this path: /root/.nvm/versions/node/v16.19.0/bin/node

So I've simply created a symlink for this executable: ln -s $(which node) /usr/bin/node16

Finally I switched back the version: nvm use system

Now you can use your default node Version with node and the desired node-version for this command with node16.

Upvotes: 1

Related Questions