NODEJS
Install
nvm
nvm allows you to quickly install and use different versions of node via the command line.
# Install
NVM_VER=v0.40.4
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/$NVM_VER/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/$NVM_VER/install.sh | bash
source ~/.bashrc
nvm --version
# If ~/.bash_profile exists
# ~/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# List remote available, only LTS version
nvm ls-remote --lts
# Set version
NODE_VER=v24.16.0
# Install target version
nvm install $NODE_VER
# Use target version
nvm use $NODE_VER
# nvm keeps "forgetting" node in new terminal session
nvm alias default $NODE_VER
# List available
nvm ls
# Use target version to run js
nvm run $NODE_VER app.js
# Remove unused version
nvm uninstall $NODE_VER
# Remove current node
rm `which node`
## Upgrade
(
cd "$NVM_DIR"
git fetch --tags origin
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
) && \. "$NVM_DIR/nvm.sh"
npm
Use version of npm in the node LTS version.
# Downgrade to old version
npm install npm@latest -g
# Remote npm -v or pm2 -v work, but always npm/node/pm2 command not found when pm2 deploy
sudo ln -s /root/.nvm/versions/node/v22.15.0/bin/npm /usr/bin/npm
sudo ln -s /root/.nvm/versions/node/v22.15.0/bin/node /usr/bin/node
sudo ln -s /root/.nvm/versions/node/v22.15.0/bin/pm2 /usr/bin/pm2
sudo rm -f /usr/bin/npm
sudo rm -f /usr/bin/node
sudo rm -f /usr/bin/pm2
sudo ln -s $(which npm) /usr/bin/
sudo ln -s $(which node) /usr/bin/
sudo ln -s $(which pm2) /usr/bin/
# Repairing broken symbolic link
# A symbolic link may become broken due to updating/compiling/installing software, # showing up as red text in ls Terminal.
readlink -v /usr/bin/npm
# Find and relink where link pointed to:
sudo ln -sfn /root/.nvm/versions/node/v22.15.0/bin/npm /usr/bin/npm
sudo ln -sfn /root/.nvm/versions/node/v22.15.0/bin/node /usr/bin/node
sudo ln -sfn /root/.nvm/versions/node/v22.15.0/bin/pm2 /usr/bin/pm2
# Initial project
npm init
# Check dep node version
npm config list
npm audit fund
npm audit report
# close funding info
npm config set fund false
# Update installed global package
npm update -g
# There should not be any output after updating.
npm outdated -g
# Find the version of an installed npm package, -g globally
npm list
# Remove no need package
npm prune
Quick Command
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -`
`sudo apt-get install nodejs
apt install nodejs=0.10*
# Update to a new major version all the packages
npm install -g npm-check-updates
# upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version.
ncu -u
# If you just downloaded the project without the node_modules dependencies and you want to install the shiny new versions first, just run
npm install
# Install or upgrade specfic package in the package.json
npm install package_name --save
# To check if any module in a project is 'old'
npm outdated
# Update audit to wanted version:
npm update
# To update package.json version numbers, append the --save flag
npm update --save
# Updating globally-installed packages
npm update -g
# Cheat Sheet: 6 Commands To Help You Update npm Packages
# This cheat sheet will make it easy to safely update npm packages in your node application. It includes a list of commands that will help you keep up with the latest updates and avoid breaking changes.
npm list --depth 0 # list all the packages in your package directory
npm audit # find out which of your npm dependencies are vulnerable.
npm outdated # list the packages that are out of date with respect to what is installed in package.json
npm update package_name # update an individual package that has already been installed.
npm uninstall package_name and npm install package_name@version # revert to a specific version.
npm cache clean --force # clear npm's cache of all the packages that have been installed.
# Windows build tool
npm i -g windows-build-tools
# Install old 12.x directly
sudo curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get -y install nodejs
# Stack size, default: 984 (kBytes)
node --v8-options | grep -B0 -A1 stack-size
-D, --save-dev: Package will appear in your devDependencies.
JEST Cannot find module
Because of JEST not suppot path alias
Use JEST unit test error:
Cannot find module 'src/index’ from 'index.js'
jest.config.js add:
const customJestConfig = {
moduleNameMapper: { "@src(.*)$": "<rootDir>/src/$1", },
}
NodeJS Express
npm install express --save
Expired
#Generate express template
npm install express-generator -g
express --view=pug project1
cd project1
npm install
vi bin/www #Change port at normalizePort(process.env.PORT || '3000');
DEBUG=project1:* npm start
#Visit http://localhost:3000/
forever
forever list
forever start dist/app.js
forever restart dist/app.js
forever stop 38282
forever stopall
Package
npm install --save express
npm install --save-dev nodemon
npx npkill
npm i -g npkill
npx npkill --delete-all --directory C:\Temp
# concurrently
# Run multiple commands concurrently.
npm-check-updates
npm install -g npm-check-updates
ncu
ncu --upgrade # ncu -u
ncu --interactive # ncu -i
ncu --interactive --format group
npx npm-check-updates
npx npm-check-updates -u
npkill
# Easily find and remove old and heavy node_modules folders
npm install npkill -g
npx npkill
npx npkill -D -x
Mock Service Worker (MSW)
npm install msw --save-dev
Jest
# Specific environment
jest --env=node
npx jest --showConfig
npx jest --coverage
npx local-ssl-proxy
npx local-ssl-proxy --key localhost+2-key.pem --cert localhost+2.pem --source 3001 --target 3000 --hostname localhost
Concept
How do I use relative URLs in Node.js?
You don’t. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the window.location object).