If nvm is already adopted on your team – great! It’s a step in the right direction, also, good amount of CI providers have support for it (e.g. Bitrise).
Add an .nvmrc file with the following content: 10.16.3 (this is current LTS version, but probably won’t be by the time you read this).
Now, you want to have a .node-version file with the same content. But although copy/paste is the best tool in a developers toolbox, it’s not the right tool for our job.
You want to symlink .node-version to .nvmrc, in order to do so, execute the following command in the root directory of your repo:
ln -s .nvmrc .node-version
Now, if you execute ls -la then you should something like that:
❯ ls -l .node-version
lrwxr-xr-x 1 steliyan staff 6 Oct 5 19:02 .node-version -> .nvmrc
You’ve successfully symlinked .node-version to point to .nvmrc!
Conclusion
Don’t rely on the aliases that nvm supports!
Updating this file takes a minute! Testing those changes will take more time, but that’s to be expected! It may just mean to run all the integration tests locally or on your CI server. In the long term – this will save your time, and probably your ass!
The builds or environments are not reproducible. It may be OK for an evening project (but don’t forget about the psychopath who knows where you live!), but for production environment it’s absolutely not recommended! Something is going to fail when a trivial fix is being deployed. You can pray that to be the build, because more subtle errors may creep into production! However, having a broken build when you want to deploy a business-critical fix/feature is no fun.
Try answering this one question – would you produce a production build of your app without using a yarn.lock or a package.lock file? Yep, I thought so. 🙂
In your project, create a.nvmrc file to add the node version. You can use the nvm —-help to check other options. In this tutorial, we are going to use node version 8.9.0.
touch .nvmrc
Add this line 8.9.0 to the .nvmrc file.
Afterwards, run the commands below:
nvm use
nvm install
nvm exec
nvm run
nvm which
nvm use looks for the .nvmrc and utilizes it. Remember, no trailing . spaces are allowed. A new line is required.
How to fix: npm ERR! enoent ENOENT: no such file or directory, rename
I recently struggled for a while with an npm error thrown when executing npm install of some package. The error message was npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -> 'D:\\WebApp\\node_modules.lz-string.DELETE'
Finally, I found a solution and a reason for that issue.
The error occurred when I was trying to install @testing-library/react npm package. It looked like that:
Try deleting package-lock.json
re-run npm install
Hotfix
If you ever get this error, the hotfix is to follow these steps:
delete node-modules folder
run command npm cache clean --force
run command npm install
install the package again with npm install your-package-name
It should all work fine after that. But it’s only a hotfix, a solution for now to unblock you.
Running npm config ls -l will show you all the implicit settings for npm, including what it thinks is the right place to put the .npmrc, as this is environment/operating system dependant. But if you have never logged in (using npm login) it will be empty. Simply log in to create it.
Another thing is #2. You can actually do that by putting a .npmrc file in the NPM package's root. It will then be used by NPM when authenticating. It also supports variable interpolation from your shell so you could do stuff like this:
Get the auth token to use for fetching private packages from our private scope
; see http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
; and also https://docs.npmjs.com/files/npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Note this line Creating default alias: default -> lts/* (-> v10.16.3). This indicates that nvm has set lts/* as the default alias. Practically this means that anytime you start a new shell, and the nvm.sh script is sourced, it will default that shell to using the installed lts release. You can change this behavior using the nvm alias command.
Example to set the default version of node to use when starting a new shell to 10.0.0:
nvm alias default 10.0.0
Use nvm to install other versions of Node.js
The real benefit of nvm comes when you install different versions of Node.js. You can then switch between them depending on which project you're working on.
I have a project that uses node.js 14.16.0. Both me and my teammember use that version. I use npm 6.14.11 and my teammember uses npm 7.x.x. This results in the package-lock.json being different; the lockFileVersion property is 1 on my PC but 2 on theirs.
I already use nvm and am considering to add a .nvmrc so everyone always uses the same node version, but this doesn't fix the npm version issue.
I believe that it is a good idea to use the npm version that the installed node.js version provides. If the next big LTS release uses a new npm version, the project will switch to that. But on NPM's site they say:
npm is a separate project from Node.js, and tends to update more frequently. As a result, even if you’ve just downloaded Node.js (and therefore npm), you’ll probably need to update your npm. Luckily, npm knows how to update itself!
Which makes me believe I should always update.
But they also say:
Node.js has lots of versions! To use Node.js, and therefore npm, effectively, you’ll want to make sure that you are on a version that is supported by the Node.js team. In general, you should use the version of Node.js labelled “LTS”.
Which makes me believe I shouldn't update and just use the one node.js provides.
What is the best practice? Does NPM not have a concept of LTS?
npm has a concept of LTS. In fact, they tag a release lts so you can npm install -g npm@lts and get the latest lts version, which as of this writing is 7.6.3.
npm will continue to support any major version of npm as long as it shipped with a version of node that is still supported. So they will support npm@6 until 14 goes EOL because npm@6 shipped with version 14.
That said, npm@7 is the current version of npm and it too will be supported on 14 as well for as long as 14 is supported.
If in doubt, use npm@lts (which is version 7 as of this writing). However, if you don't really care one way or the other and don't want to force your coworker to update, npm@6 will continue to receive updates as long as Node.js 14 is supported. I would recommend updating to the latest npm@6 with npm install -g npm@6 though. Either version (npm@6 or npm@7) should work just fine. You just need to pick one with your coworker to avoid the package-lock.json churn (or not care about the lockfile churn
NPM PACKAGES (control version)
If you have to install an older version of a package, just specify it
npm install <package>@<version>
For example: npm install express@3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
Last update on May 25 2020 13:26:02 (UTC/GMT +8 hours)
If you need to use private packages, your nom version must be greater than 2.7.0. and you also have to be a paid npm user.
Npm private packages, enable you to use the npm registry to host your own private code and the npm command line to manage it. This will make it easy for you to use public packages such as Browserify and Express side-by-side with your private code.
First steps (Before we start)
The first thing to do is to log in to npm again once you have upgraded to the most recent version.
npm install -g npm
npm login
Setting up your package
Every private package is scoped. If you have a package whose name begins with @, then that package is a scoped package. The scope consists of everything that is between the @ and the slash.
@scope/project-name
As an individual user, when you sign up for private modules, your scope will be your username. In the case where you created an npm Org, the orgname could be the scope. Orgs can either be free or paid. The only free way to use scopes in package names is to use public Org.
@username/project-name
@orgname/project-name
If you used npm init to initialize your packages, you use the command below to pass in your scope:
npm init --scope=<your_scope>
In the case where you are using the same scope most of the time, you probably will want to set the command above in your default configuration like this:
npm config set scope <your_scope>
Publishing your package
It is easy to publish your package; all you need to do is to run the command below:
npm publish
Scoped packages are published as private by default. Once you have published it, you should be able to see it on the website with a private flag.
Sharing access from the web
If you want to grant access to someone, they have to be subscribed to private packages as well. Immediately they are, you can then grant them read-write access. Or you could set up an npm Org that will give them access through Orgs and teams.
There are 2 versions of node.js – LTS and latest current version. When using npm install to install packages, are the packages installed independent of the node.js being used? Do different node.js versions install different versions of the packages?
No they don’t. When you install a npm package, it has its own version but of course package’s version and node’s version affect the usability of these packages.So, you need to delete old version of node from your system files. One more thing, please do not install npm packages global.Because when you do that, next time maybe you can use it in a other project but when it is updated by creater, you won’t be able to have new updates and features or as I said before it can’t be compatible with the new version of node.
EDIT: several years since this question was first answered, as noted in a newer answer, there is now a command for this:
nvm now has a command to update npm. It's nvm install-latest-npm or nvm install --latest-npm.
nvm install-latest-npm: Attempt to upgrade to the latest working npm on the current node version
nvm install --latest-npm: After installing, attempt to upgrade to the latest working npm on the given node version
Below are previous revisions of the correct answer to this question.
Over three years after this question was first asked, it seems like the answer is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.
I just installed node 4.2.2, which comes with npm 2.14.7, but I want to use npm 3. So I did:
cd ~/.nvm/versions/node/v4.2.2/lib
npm install npm
Easy!
And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node.
EDIT 1: In the newest version, npm -g is smart and installs modules into the path above instead of the system global path.
The main difference between local and global packages is this:
local packages are installed in the directory where you run npm install <package-name>, and they are put in the node_modules folder under this directory
global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>
In your code you can only require local packages:
JScopy
require('package-name')
so when should you install in one way or another?
In general, all packages should be installed locally.
This makes sure you can have dozens of applications in your computer, all running a different version of each package if needed.
Updating a global package would make all your projects use the new release, and as you can imagine this might cause nightmares in terms of maintenance, as some packages might break compatibility with further dependencies, and so on.
All projects have their own local version of a package, even if this might appear like a waste of resources, it's minimal compared to the possible negative consequences.
Versioning is an important part of npm and how to use updates safely when developing web applications.
Most npm packages follow semantic versioning guidelines.
Semantic versioning means that developers should compose a package version of three numbers separated by periods (e.g., "0.12.31").
MAJOR.MINOR.PATCH versioning format
The first number, called the major version, indicates how significant a release this is in relation to other releases with the same minor and patch levels. Major version number indicates incompatible API changes.
The second number, called the minor version, indicates how much new functionality has been introduced since the last significant release; for instance, if this change was only small fixes or enhancements to existing features and no behavior changes were made then it would result in a higher value. Minor releases are not as risky as major version because they typically introduce new features, but they are not as risky as major updates because no API changes were made.
The third number is called the patch version and it indicates how much bug fixes or enhancements have been introduced since the last minor release; for instance, if this change was only small fixes or enhancements to existing features and no behavior changes were added.
What do the caret (^) and tilde (~) mean?
In package.json, a version can have a ^ in front (e.g. ^0.12.31), meaning the latest minor release may be safely installed.
Tilde (~) in front (e.g., ~0.12.31) means the latest patch release is safe to install.
An example of npm packages that are listed as dependencies in the package.json file. All dependencies have a caret (^) in front, showing that it is safe to install the latest minor versions.
package.json package.json is a file that keeps track of all the packages your app needs to run properly, as well as settings for how it should behave when running on different platforms and environments.
Step 2: Audit installed npm packages
Before you update npm packages, figure out if you have a good reason to.
It is better to stick with the package version that works. That way you will not have a risk of something breaking.
Primary reasons for upgrading npm packages are:
Recent version of the package having a feature that we want
Fixed bugs in the latest version of an npm package
Updated dependencies for another package that you are using
A security vulnerability in the npm package
Upgrade of the environment where the project is running is not compatible with the the current version of the npm package
Couple of npm commands that will help you audit your packages before upgrading:
npm list --depth 0 lists all packages at the top level
npm audit checks for security vulnerabilities or out-of-date versions
npm outdated lists report of package versions compared to versions specified in package.json file
npm list –depth 0
npm list --depth 0 lists all installed npm packages, but only at the top level.
Listing packages at the top level is enough most of the time. Top-level dependencies usually take care of their inner dependencies.
npm audit
npm audit will run a security vulnerability check against your project and report any found issues. It is not perfect, but it helps to find potential problems if you are using npm packages that have security vulnerabilities. It's not perfect because not all vulnerabilities are reported to npm.
npm audit shows you a list of vulnerable packages, including the npm dependency tree.
npm outdated
npm outdated will report any out-of-date packages in your project.
It shows current, wanted and latest versions compared to versions specified in package.json file.
Current: is the currently installed version.
Wanted: The maximum version of the package that is allowed by the version range in package.json.
Latest: version of the package is the one that is tagged as "latest" in the npm registry.
Note: npm outdated command only shows the direct dependencies of the root project. But if you want to see other dependencies also, then use "–all."
npm outdated prints out a list of all installed packages that have updates available.
Check for breaking changes before you update
Some npm packages will introduce breaking changes, which may cause errors when using the module.
Before making a breaking change, package developers often add "Breaking Changes" messages to the console output. It means that the module will change in future versions and developers need to keep an eye out for it.
To see if there are any breaking changes, you can also look at the "Breaking Changes" section of the package's readme file.
inside of a module directory, check node_modules folder inside of your project
project's website (or GitHub)
Step 3: Update only one package at time
When updating, we need to be careful to only update packages we want. There is no need to update all of your modules at the same time.
Start by making updates in small batches and test each batch for any issues that might arise. This will allow you to find out how it's affecting your project, and it will let you isolate any errors.
npm update
Changing the package version in package.json file and running npm install will most likely not do anything because already installed package version satisfies the versioning in the package.json file.
Rather than using npm install, you can use the npm update command to upgrade already installed packages. When you run a npm update, npm checks if there are newer versions out there that satisfy specified semantic versioning ranges that you specified in package.json and installs them.
To update a specific npm package, run the following in console:
npm update package_name
How to revert npm package updates?
If there are any bugs, you can easily undo the changes with these two commands:
The @version should be the same version that you had installed previously.
Step 4: Test your code after installing new packages
In order to make sure your code still works after updating npm packages, it's important that you test the functionality before deploying. This is because a package update may cause errors in your application if you are not careful. To avoid these issues, I recommend running all tests on server and client side as well as manually checking for any JavaScript error messages throughout the site.
Steps:
Run all unit and integration tests from both serverside and clientside by running npm test or equivalent command for your project.
Review package logs for clues about what caused an issue or where things went wrong during installation
These 3 simple steps can help you avoid breaking your project by carefully installing new npm packages.
What are some of the other ways that people have broken their projects? Let us know in the comments below, and we'll write a blog post on them!
Bonus Tip: Clear npm cache
As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead. On the other hand, if you're debugging an issue with the installer, you can use npm install --cache /tmp/empty-cache to use a temporary cache instead of nuking the actual one.
Sometimes npm doesn't pull the latest version of the package because it has an older version stored in cache. As of npm@5, cache issues should not be happening. But they still do sometimes.
To clear npm cache, run npm cache clean --force. This command clears npm's cache of all the packages that your project has installed with npm install or npm update.
It does not remove any dependencies from package.json, but it may help resolve a dependency issue if there is an outdated version in the cache and you can't find which one it is by looking through the packages list.
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.
Use npm list --depth 0 to list all the packages in your package directory
Use npm audit to find out which of your npm dependencies are vulnerable.
Use npm outdated to list the packages that are out of date with respect to what is installed in package.json
Use npm update package_name to update an individual package that has already been installed.
Use npm uninstall package_name and npm install package_name@version to revert to a specific version.
Use npm cache clean --force to clear npm's cache of all the packages that have been installed.
Node.js 15 was released on October 20, 2020. It comes with npm 7 and many new features. Are you ready to try it out?
But wait a minute: Node.js 15 and npm 7 come with breaking changes. Will the upgrade mess up your existing projects?
Yes, potentially it could.
Luckily, we have NVM (Node Version Manager), which can help us mitigate the risk. Let’s take a walkthrough nvm and be confident enough to upgrade node.js and npm versions.
Install NVM
nvm manages node.js and npm versions. It’s designed to be installed per-user and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
The script, install.sh, clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
The arrow in the above output shows that the current version of node.js is 10.14.0. It also shows the values for default (12.16.0), node (15.4.0), and stable (15.4.0).
nvm use changes the current version:
$ nvm use 12.16.0
Now using node v12.16.0 (npm v6.14.8)
$ nvm use 10.16.0
Now using node v10.16.0 (npm v6.14.5)
$ nvm use 13.9.0
Now using node v13.9.0 (npm v6.13.7)
$ nvm use default
Now using node v12.16.0 (npm v6.14.8)
$ nvm use node
Now using node v15.4.0 (npm v7.0.15)
$ nvm use stable
Now using node v15.4.0 (npm v7.0.15)
You may wonder how v10.16.0 uses a later version of npm than v13.9.0. This can be achieved with the following commands:
$ nvm use 10.16.0
$ npm install -g npm@6.14.5
The following command will get the latest supported npm version on the current node version:
$ nvm install-latest-npm
nvm use sets a specific version for the current shell. If you start a new shell, the newly set node.js version will be lost.
How can we make a specific node version persistent?
The default version is the one that carries over to all shells.nvm alias can set the default version.
$ nvm alias default 10.16.0
A .nvmrc file can be created for convenience, which takes SemVer format, or node, or default. Afterward, nvm use, nvm install, nvm exec, nvm run, and nvm which will use the version specified in the .nvmrc file if no version is supplied on the command line.
$ cat .nvmrc
15.4.0
$ nvm use
Found '/Users/fuje/.nvmrc' with version <15.4.0>
Now using node v15.4.0 (npm v7.0.15)
We can check the current version with the following command:
$ nvm current
v15.4.0
ls-remote lists all available versions, but be prepared for a very long list.
$ nvm ls-remote
More specifically, providing a partial version can narrow down the available list.
nvm which shows the path to the executable to where it was installed. We have installed node.js of 10.14.0, 10.15.0, and 10.16.0. Here is nvm whichresults:
$ nvm which 10.14.0
/Users/fuje/.nvm/versions/node/v10.14.0/bin/node
$ nvm which 10.15.0
/Users/fuje/.nvm/versions/node/v10.15.0/bin/node
$ nvm which 10.16.0
/Users/fuje/.nvm/versions/node/v10.16.0/bin/node
$ nvm which 10.15
/Users/fuje/.nvm/versions/node/v10.15.0/bin/node
$ nvm which 10.12
N/A: version "v10.12" is not yet installed.
You need to run "nvm install 10.12" to install it before using it.
$ nvm which 10
/Users/fuje/.nvm/versions/node/v10.16.0/bin/node
A specific node version can be used directly to run an app:
$ nvm run 10.15.0 app.js
Alternatively, the following command runs node app.js with the PATH pointing to node 10.15.0.
$ nvm exec 10.15.0 node app.js
If you want to find more nvm commands, run the help command:
$ nvm --help
Upgrade NVM
We can use nvm to upgrade node.js and npm. How can we upgrade nvm itself?
Let’s try.
Before the upgrade, we have nvm0.34.0.
$ nvm --version
0.34.0
We upgrade it to version 0.37.2.
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13527 100 13527 0 0 23046 0 --:--:-- --:--:-- --:--:-- 23083
=> nvm is already installed in /Users/fuje/.nvm, trying to update using git
=> => Compressing and cleaning up git repository=> nvm source string already in /Users/fuje/.bash_profile
=> bash_completion source string already in /Users/fuje/.bash_profile
=> Close and reopen your terminal to start using nvm or run the following to use it now:export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
As the output stated, we need to close and reopen the terminal to use the new version:
$ nvm --version
0.37.2
Compared to version 0.34.0, version 0.37.2 added the feature of nvm set-colors for console output.
By default, nvm ls shows the following colors:
Set to new colors:
$ nvm set-colors cgYmW
nvm ls displays the output with new colors:
Conclusion
nvm makes it easy to manage node.js and npm versions. We’re ready to upgrade to node.js 15 and npm 7. You can read the two articles below for more details.