What is the NPM?

NPM is a package management tool for javascript, which is a signature product of front-end modularization. Simply put, it is through NPM download modules, reuse the existing code, improve work efficiency.

1. From the perspective of the community: publish modules for a specific problem to the NPM server for others in the community to download and use, and at the same time, find resources for specific modules in the community to solve the problem 2. From the team’s point of view: With NPM, a package management tool, it is much easier to reuse the team’s existing code

Utilize the NPM installation package

How NPM is installed – local and global

When to install locally/globally?

  1. When you try to install command line tools such as grunt CLI, use the global install global install method: NPM install -g module name
  2. Use local installation when you try to install a module via NPM and import it via require(‘XXX’)

Local installation: NPM install Module name

For example, I tried to install express globally for the first time, typing NPM install -g express

And what makes you speechless is that you have to install a lot of dependencies before reminding you that you don’t have enough permissions…

Solution:

  1. Sudo NPM install -g XXX (You could also try using sudo, but this should be recommended)
  2. Sudo chown -r NPM directory /{lib/node_modules,bin,share} The official recommended practice, chown, stands for change owner, is to specify the owner of the NPM directory as your name (granting permissions), and -r means to do the same for all subdirectories and files in the specified directory.

<1> First, get the path to the NPM directory through NPM config get prefix, for example:

<2> On the command line, type sudo chown -r NPM directory directory /{lib/node_modules,bin,share}, for example:

[Note] {lib/node_modules,bin,share} braces are to be written on the express: NPM install -g express

Successful installation

  1. Sudo CHmod 777 NPM directory (not recommended)

Evaluation: This is a common solution found on the Internet, but it is not mentioned in the official tutorial. Chmod stands for change mode to change the read/write mode, granting the directory the highest permissions, and allowing anyone to read or write to it, which is dangerous

When installed locally, dependency package information is written to package.json

Note that a common scenario in teamwork is for someone to clone your project from Github and then install the necessary dependencies via NPM install. These are dependencies and devDepencies in your package.json. Therefore, it is important to write dependent package information (required name and version) to package.json while installing locally!

NPM install module: Json < span style = “box-sizing: 0px; line-height: 21px! Important; word-break: break-word! Important; Once installed, write to devDepencies in package.json (development environment dependent)

Use NPM to delete packages

Removing modules is simple: Remove global modules NPM uninstall -g Remove local modules NPM uninstall modules

What you should think about when removing a local module: Will the dependency information on package.json also be removed? NPM uninstall module: NPM uninstall module –save Delete module, NPM uninstall module –save-dev deletes the module from package.json and devDependencies in package.json

Distribute packages using NPM

To publish a package for the first time, enter NPM adduser on the terminal, prompting you to enter your account, password and email address, and then prompt you to create a package for the first time: Enter NPM login on the terminal, then enter your account, password, and email address, and login [note] NPM adduser is successfully logged in by default, so there is no need to follow NPM login.

Example: (because I already created the account, so I login directly)

  1. Enter the project directory and log in again:
  2. Publish via NPM. The package name and version are the same as the package.json name and version in your project.
  3. Then you can go to NPM search to find the published APP!

A few points to note:

Cannot have the same name as an existing package!

NPM restrictions on package names: no uppercase letters/Spaces/sliders!

Do you have some private code in your project that you don’t want to publish to NPM? Put it in.gitignore or.npmignore, and the upload will be ignored

Use NPM to undo the distribution package

One thing to note here is that undoing a package is not as easy as you might think, there are a lot of restrictions, and undoing a package is considered bad behavior (think about undoing a package [assuming it has some impact in the community], How devastating this is for teams that are already heavily using and relying on the packages you release!)

Example: I now unpublish the previously published package penghuwanapp: type NPM unpublish package name

Notice the words in the red box and you can see the NPM’s official position on this action….

[note] If a permission error is reported, add –force

No more NPM searches

  1. According to the specification, unpublish is only allowed with versions published in the last 24 hours
  2. Even if you undo the package, you can no longer send the package with the same name and version as the package being revoked.

For example, IF I try to publish a package with the same name + version after canceling the package:

An error was reported and I was advised to change the package version

NPM unpublish’s recommended alternative: NPM deprecate [@] Using this command will not cancel existing packages in the community, but will warn anyone trying to install the package. For example: NPM Deprecate Penghuwanapp ‘I no longer maintain this package’

NPM update released after the package:

The NPM publish command is the same as the NPM publish command, the difference is that you need to change the version of the package

So the steps are:

  1. Change the version of the package (version field in package.json)
  2. npm publish

For details on the modified version, see below:

Versioning of NPM — Semantic Versioning

There is a version field in our package.json. So how do you adjust releases as the project is built? NPM has its own version control standard — Semantic Versioning

For “version”:” X.Y.Z”

  1. Bug fixes, minor changes, added Z
  2. Added new features, but still backward compatible, adding y
  3. There is a big change, not backward compatible, add x

For example, if my original project was 1.0.0, it would be 1.0.1 if it was 2, it would be 1.1.0 if it was 3, it would be 2.0.0