Understand node package manager – NPM

Tram Ho

The node package manager , also known as NPM , is a repository of libs, packages of the node js ecosystem; just like other repositories: NuGet from .Net, Composer for PHP, Maven from java, Pip from python, …; NPM provides maximum support for installing, uninstalling, updating, and versioning packages in a Node – javascript project. Here I would like to summarize some NPM commands used in the process of making projects.

Project initialization

A Node – javascript project is indispensable package.json file, this file will contain information of the project, run scripts, and most importantly, information about libs (also known as packages , dependences ) and version. In order to initialize this file, run the following command:

Run and fill the information according to the questions in the terminal (this information can be edited directly in the package.json file after initialization) We can ignore the input and let npm fill the information. message default with the command npm init -y

Install Libraries (libs or packages or dependences)

Depending on the purpose of use, there are several ways to install the following:

1.1. Install dependences for the project

Running the following commands will automatically save to the dependences section of the package.json file

1.2. Install dev dependences for the project

Dev dependences are packages that will be used for development purposes in the Develop environment without having to build them on the Production environment such as eslint , webpack , babel tools, etc. Run the following commands will automatically save to the devDependencies section of the file. package.json

After running the above commands, you will have the following information:

  • Package name and version in the package.json file: we will push this file to the git repo so that the members of the project will pull back and run the command npm i to install all the packages contained in this file.
  • File package-lock.json: This file will save the exact log versions of the installed package, this file needs to be pushed to the git repo.
  • Folder node_modules: This folder will contain the source code of the packages in the project. Note that never push this folder to the git repo, but another member will run the command npm i to create this folder on their local machine.

2. Install the global package

There are packages that we want to install globally , that is, they do not belong to a specific project; It can be used in any terminal folder location

Running this command will not save package information in the package.json file of the project.

3. Run the package directly without installing – npx: an NPM package runner

npx will execute the package directly from the npm registry without having to install it on a local machine, so there are a number of benefits such as:

  • Does not consume local machine memory.
  • Always execute the latest version on the npm registry.

Find out about version in package.json

How to update package version safely; what the characters ^ and ~ mean in version; How to upgrade a higher major version of the package, let’s learn next.

1. How to mark the version

NPM package version has 3 parts: Major.Minor.Patch

  • Major: This is a version increment that is intended to make major changes to the structure and behavior of the current package and possibly affect projects that are using them (or breaking changes ).
  • Minor: This is a version enhancement that aims to add new features without changing the structure, the way the current packages work and without affecting the projects that are using them.
  • Patch: This is a version enhancement that aims to fix bugs, fix small parts without changing the structure and way of operations of the current package and not affecting the projects that are using them (or the patch. ).
2. Learn the ^ and ~ symbols in version

The symbol ^ before version. This means you can safely update to the latest minor version without fear of your app crashing or being affected. For example the current version in package.json is ^15.2.1 , you can safely update to the latest version ^15.9.4 , as long as it is ^15.x

The ~ symbol before version. This means you can safely update to latest pacth version without fear of app crashing or being affected. For example the current version in package.json is ~15.2.1 , you can safely update to the latest version ^15.2.15 , as long as it is ^15.2.x

3. Update npm packages version
3.1 See all versions of the package:

3.2 View the latest version of the package:

3.3 Check and update out of date packages:

As shown above, it will list out information such as:

  • Current: version currently in use in the project.
  • Wanted: latest safe version (prefix ^ or ~ in version) that we can safely update.
  • Latest: latest version on the npm registry .

So we need to update all outdated packages to the Wanted version as follows

3.4 Update latest major version – breaking changes

To safely update to the latest version, the first thing we need to do is check the package changelog (home page doc release note or github release note) to see what has changed, what new additions to evaluate. the level of impact on the project being used.

After updating the latest version, we need to check the operation of the app and update the changes according to the new version of the package. Update according to the above commands will reflect the file package.json , package-lock.json , folder node_modules .

Conclude

I have listed some things to keep in mind when using NPM, hoping to help everyone when working with NPM. Thanks for reading the article!

Share the news now

Source : Viblo