October 25

How to Update npm Packages to Their Latest Version

0  comments

JavaScript (JS) has been the most popular programming language and one of the most effective tools for creating dynamic, sophisticated websites and web applications for years now. It offers a wide range of libraries and resources to accelerate the speed of website/application development. However, this expansive library eventually needed an effective way to manage and manipulate its content.
This is why we got Node Package Manager, more commonly known as npm, for the sake of convenience. The npm is a front-end package manager routinely used with the node.JS framework to manage project dependencies.

Are you looking to update npm packages and not sure how to go about it?

Read on for a step-by-step, easy-to-follow process of doing just that. But first, let’s discuss the basics of using npm.

Introduction to npm for beginners

What is the Node Package Manager?

Node Package Manager (npm) is widely viewed as the default Node.js package manager.Home to more than a million code packages, it is often considered the largest single-language software registry in the world.

The npm is an open source and simplifies the process of managing the vast repository of Node.js packages.The npm is almost completely written using javascript.

You can install the npm by using Node.js, which means you need to install Node.js first if you want to have npm running on your computer. To download Node.js, go to the official website nodejs.org. All npm code packages are detailed in the package.json file. You can share your own code packages with the npm registry at npmjs.com. The npm may be the largest, most comprehensive, and best known, but it is not the only node.js package manager there is. Other options include Yarn, pnpm, and Bower.

List of npm components

There are three key parts of npm:

  • The Website - The official nmp website is npmjs.com. Here, you can locate the code packages you need along with relevant documentation. In addition, through this site, you can publish and share your own code packages.
  • The Registry - The npm registry is a database of more than a million code packages. Developers can publish their code packages to the registry or download existing ones.
  • The CLI - After you have identified a package you need, you then need to install it. NPM comes with a command line client (CLI) you could use to download and/or install code packages. It is a means of interacting with the npm.

Common uses for the Node Package Manager

  • Manages code package download
  • Updating the version of installed code packages
  • Manages dependencies and can install all of a project’s dependencies using one command line. It can manage both code packages representing local dependencies as well as globally installed JS tools
  • Manages versioning, thus allowing you to install the version of your code package that is compatible with the one you are using. You can install the current or a lower version. If you do not specify the version, the current version will be installed automatically
  • Allows you to adapt a code package to your application’s requirements
  • Choose from multiple code packages, each of which can perform the task you want to complete
  • With npx, you can run code packages without necessarily downloading them
  • Share your code package with other npm users
  • Limit the code package to a specific developer to safeguard its integrity
  • Create organizations and teams to manage your code packages
  • Provides a means of reaching out to developers working on similar projects

Installing minor and major updates to npm packages

Updates to your code package help keep your applications and tools usable, secure and stable. It ensures your application benefits from any improvements to code package dependencies. You can either run a minor update or a major update. A minor update is for an individual code package, whereas a major update is for multiple global packages.

1. Installing minor updates (Individual)

Any time you install a code package via npm, the most current version is downloaded by default to the node_modules folder. Subsequently, an entry is made to both package.json and package-lock.json within the current folder.

Therefore, to update local packages, go to the root directory of your project folder on your computer. While here, confirm that there is a package.json file in this directory. 

For minor updates and patches to local code packages, run “npm update --save” while within the project directory. You can use “npm update --no-save” if you do not want changes made to package.json.

For more significant updates to local code packages, type the command “npm update” and run it.

You can check that the code packages have been updated successfully by running the command “npm outdated”. There should be no output if your code packages are all up to date.

2. Installing major updates (Global)

The previous process will work for minor updates and updates to local code packages. However, the process is different for major updates to global code packages. 

To run a major update to global code packages, check if there are global packages that need updating. Type and enter the code “npm outdated -g --depth=0” in your command line. 

To update one global package, run the command “npm update -g <package_name>”.

To do the same for all global packages, run “npm update -g”. 

To upgrade package.json dependencies to the most current version while disregarding any specified versions, run “npm install -g npm-check-updates”.

Known issues during npm updates

Some of the known issues around npm updates include the following:

  • In case ncu generates output that does not seem to correspond to the package, it may be in conflict with one or more executables such as Nvidia CUDA or ncu-weather-cli. To get around this, use the full name instead i.e., “npm-check-updates”.
  • In case the npm-check-updates command hangs on MS Windows, explicitly set the package file. That is, “ncu --packageFile package.json”. You may run “ncu –loglevel verbose” to establish if it was erroneously waiting for stdin.

Using doctor mode to identify breaking updates

You cannot use npm update to update major code package releases. Major releases cannot be updated with npm update since there is a risk of introducing drastic changes that could break your application. 

npm --doctor is a useful utility that confirms the npm environment is running as it should. It performs multiple checks, such as a ping check to ascertain if your PC is connected to the registry.npmjs.org (that is, the npm registry). Doctor mode also establishes the version of npm and node.js and checks the path to the git program.

List of command options for npm

Some of the most used command options for npm include the following:

  • --cwd <path> : The working directory where npm is going to be executed.
  • - doctor u : Installs upgrades iteratively to find upgrades that could break your application.
  • --doctorInstall <command> : Identifies the script to use in doctor mode.
  • --doctorTest <command> : Identifies what script to use in doctor mode.
  • --enginesNode : Include just the packages that meet engines.node as indicated in the package file.
  • - f, --filter <p> : Include only the packages that satisfy a specific wildcard, string, glob, predicate function, or space/comma delimited list.
  • --format <value> : Modify formatting or output or display additional information. You can indicate at least one comma-delimited value. 
  • -g, --global : Address global packages instead of code packages within the current project.
  • -h, --help : Show help for a command.
  • -i, --interactive : Run interactive prompts per dependency.
  • -j, --jsonAll : Generate a new package file, and not a human-readable text.
  • --jsonDeps : Similar to jsonAll, except it's limited to listing optionalDependencies, devDependencies and dependencies.
  • -m, --minimal : Prevents upgrade to newer versions if current version meets version range as per semver.
  • --packageFile <path|glob> : Location of package files with the default being ./package.json.
  • --peer : Confirm peer dependencies for installed code packages. Filter updates to only include versions proven as compatible.
  • --prefix <path> : The current working directory for npm.
  • -r, --registry <uri> : For a third-party npm registry.
  • -x, --reject <p> : Do not include code packages that match the particular wildcard, string, glob, predicate function, /regex/ or space-/comma-delimited list.
  • --rejectVersion <p> : Exclude versions of package.json via predicate function, space-/comma-delimited list or /regex/.
  • --removeRange : Get rid of version ranges from the final version of the code package.
  • --retry <n> : The number of times to re-attempt failed requests for code package information.
  • --root : Perform updates to a root project as well as the workspaces indicated.
  • -s, --silent : Don’t generate any text.
  • -t, –target <value> : Perform upgrade to a specified version. This could be the newest, latest, greatest, patch, minor, etc.
  • -u, --upgrade : Overwrite the code package files with updated versions instead of sending output to the console.
  • -v : This checks the installed version of npm.
  • -w, --workspace <s> : Execute in at least one workspace specified.
  • -ws, --workspaces : Execute in every workspace.

Conclusion

Node.js has rightfully earned a stellar reputation for building quality websites and sophisticated web applications. Node.js installation comes with npm by default. npm helps you manage the dependencies and packages of Node.js. In this concise guide, we managed to present the basics of npm and how you can update npm packages to their latest versions. Follow these best practices to ensure npm is up to date and working perfectly.


Tags


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get in touch

Name*
Email*
Message
0 of 350
>