Skip to content

sahilrajput03/nvm-autoswitching

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

README

NVM Docs: Docs

Inspiration: here

Commands

Using .nvmrc

## Creating a `.nvmrc` file in current working directory
node -v > .nvmrc

## Install node version mentioned in .nvmrc file in current working directory
nvm install
# Note: If there is no .nvmrc file then you get error - "No .nvmrc file found"

## Switch to Node version mentioned in .nvmrc in current working directory
nvm use

General usage

# ✅ Insatll node LTS (Long-term Support)
nvm install --lts

# Insatll specifc version
nvm install 22.13.0
nvm install 20.12.0
nvm install 16.14.2
nvm install 12
nvm install 14

# Install `latest` version
# *[PLEASE DO NOT USE `latest` FOR PRODUCTION PROJECTS.]*
nvm install latest
# or (`node` is an alias for `latest`)
nvm install node


# List of installed node versions
nvm list


# Set default node version
nvm alias default v22.13.0	# 14 Jan 2025 (latest - using this on macos)
# nvm alias default 20.12.0	# (older)

# ❤️ Switch to defautl node version set in nvm (`check by nvm list | grep default`) in the current shell session
nvm use default

Remove/Uninstall a particular node version

nvm uninstall <NODE_VERSION>

Update npm version via nvm

Source

# Install latest npm version
nvm install-latest-npm
# or
nvm install --latest-npm

# Install specific npm version
npm install -g [email protected]

What to do to make autoswitching seamlessly?

Note: Official documentation of NVM also says about how to do this but the code they provided for .zshrc (.bashrc) didn't work in my macos as of 14 Jan 2025.

In your .bashrc (or ~/.zshrc [tested]) file you can add a statement include ~/.bash_nvm. [Note: include is a my custom bash function that allows safe sourceing i.e, only source if file exists]

And put below code in ~/.bash_nvm:

nvmUse () {
	if [ -f ".nvmrc" ]; then
		nvm use
	fi
}
nvmUse
function cd {
    # actually change the directory with all args passed to the function
    builtin cd "$@"
    nvmUse
}

what is engine-strict=true in .npmrc file (ChatGPT)

Setting engine-strict=true in Node.js enforces strict compatibility with the version specified in the engines field of your package.json file. Here’s how it works:

Purpose: By default, the engines field in package.json specifies compatible Node.js and npm versions, but it doesn't prevent installation if there's a version mismatch. However, if you set engine-strict=true, it will make these specifications mandatory, meaning npm will throw an error if the Node.js or npm version doesn't match the specified versions.

Use Case: This is useful for ensuring that your project is only run on compatible versions, reducing the risk of unexpected errors due to version differences.

How to Set It: You can add this flag in your .npmrc file like this:

engine-strict=true

Or, you can set it as a one-time configuration by running:

npm config set engine-strict true

When enabled, if a user tries to install or run the project with an incompatible Node.js version, npm will prevent the installation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published