npm and yarn

Installing npm and NodeJs

Using nvm

Install nvm using the curl command from here: https://github.com/nvm-sh/nvm

To install the latest version of NodeJs: nvm install --lts

Check Node.js version

node -v || node --version

List locally installed versions of node

nvm ls

List remote available versions of node

nvm ls-remote

Install specific version of node

nvm install 18.16.1

Install the latest LTS version

nvm install --lts

Use the latest LTS version

nvm use --lts

Set default version of node on a shell

nvm alias default 18.16.1

Switch version of node

nvm use 20.5.1

Install latest LTS version of node (Long Term Support)

nvm install --lts

Install latest stable version of node

nvm install stable

For help

nvm help

To remove, delete, or uninstall nvm - just remove the $NVM_DIR folder (usually ~/.nvm)

Issues

When I tried this method on Amazon Linux 2, I ran into the error

node: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by node)

The error is telling us that we do not have a new enough version of glibc for nvm to install node 18. The error is telling us that it requires at least glibc 2.27.

You can find your current glibc version with: ldd --version

Installing a newer version of glibc is tedious and also dangerous. It can potentially break the computer.

Amazon Linux 2 or some os includes glibc 2.26 or under. To use a higher glib version, you need to consider other AMI. for example) Amazon Linux 2022. (al2022-ami) it includes glibc 2.34

reference : https://repost.aws/questions/QUrXOioL46RcCnFGyELJWKLw/glibc-2-27-on-amazon-linux-2

If, for whatever reason (like, working in a corporate environment that support specific versions of VMs), upgrading to a new version of Amazon Linux is not a option, then this approach is not going to work.

Using homebrew

brew install node@18

After installing, the path needs to be specified correctly. Add this line to .bash_profile

export PATH="/home/linuxbrew/.linuxbrew/opt/node@18/bin:$PATH"

And refresh the file

. ~/.bash_profile

From Standalone binary

  1. Go to https://nodejs.org/en/download
  2. Download the required version of Node.js
  3. Unzip it: tar xf node-v15.6.0-linux-x64.tar.gz
  4. vim ~/.profile
  5. Add these variables
    # NodeJS
    export NODEJS_HOME=/{path_to_the_extracted_folder}/node-v15.6.0-linux-x64/bin
    export PATH=$NODEJS_HOME:$PATH
    
  6. Refresh profile
    . ~/.profile
    
  7. Check node version
    node -v
    

Upgrade npm

If you see the message, it is time to upgrade npm in your computer

new major version of npm available

Using nvm

This seems to be updating the version of node but not npm

nvm install --lts

nvm now has a command to update npm. It’s

nvm install-latest-npm

or

nvm install --latest-npm

From standalone binary

https://docs.npmjs.com/try-the-latest-stable-version-of-npm

npm commands

npm ci vs npm install:

npm ci makes sure that the version numbers from package.lock file are installed. This is a much safer version.

npm run build builds a production ready folder for our website. We can grab it and deploy it on providers like Netlify.

yarn

npm install --global yarn

npm install --global yarn@1.22.19

yarn outdated - https://classic.yarnpkg.com/lang/en/docs/cli/outdated/

yarn link - https://classic.yarnpkg.com/en/docs/cli/link

yarn upgrade - https://classic.yarnpkg.com/en/docs/cli/upgrade

npm vs yarn

TODO

yarn install pulls down all the dependencies much more effectively compared to npm install. Why is this?

What are the differences between yarn and npm?

Where is Node.js installed?

which node
which nodejs
which npm

If you are on Windows, write where instead of which

Where does npm install packages?

https://stackoverflow.com/questions/5926672/where-does-npm-install-packages

Dependency errors while creating applications

https://stackoverflow.com/questions/64573177/unable-to-resolve-dependency-tree-error-when-installing-npm-packages

[explorer436@explorer436-legion-82b1 react-playground]$ npx create-react-app my-app

Creating a new React app in /home/explorer436/Downloads/GitRepositories/programming-playground/react-playground/my-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...


added 1323 packages in 13s

267 packages are looking for funding
  run `npm fund` for details

Installing template dependencies using npm...
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: my-app@0.1.0
npm error Found: react@19.0.0
npm error node_modules/react
npm error   react@"^19.0.0" from the root project
npm error
npm error Could not resolve dependency:
npm error peer react@"^18.0.0" from @testing-library/react@13.4.0
npm error node_modules/@testing-library/react
npm error   @testing-library/react@"^13.0.0" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.
npm error
npm error
npm error For a full report see:
npm error /home/explorer436/.npm/_logs/2025-01-17T03_28_02_250Z-eresolve-report.txt
npm error A complete log of this run can be found in: /home/explorer436/.npm/_logs/2025-01-17T03_28_02_250Z-debug-0.log
`npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0` failed
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: project-admin@11.0.0
npm ERR! Found: @angular/common@11.0.3
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"11.0.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^9.1.0 || ^10.0.0" from @agm/core@3.0.0-beta.0
npm ERR! node_modules/@agm/core
npm ERR!   @agm/core@"3.0.0-beta.0" from the root project

First you should start to read the problem from the bottom to the top. Here @agm/core@3.0.0-beta.0 requires angular common 9.1.0 or 10.0.0. And the top message says that the angular common found is actually 11.0.3.

First, execute this in your terminal.

npm config set legacy-peer-deps true

Second, clear the cache:

npm cache clean --force

And finally, execute your command.

Reinstall packages after deleting node_modules

yarn install --check-files

Links to this note