1. The background of NPM

As front-end developers and Node developers, you are familiar with NPM. Commands such as NPM init, NPM install, and NPM uninstall are widely used. So what is NPM? Recently, I have been studying the book “NodeJS in Plain English” and reading some blog articles. Here I have summarized some understanding of NPM. This article records some of my thoughts.

NPM is a package management tool installed with the NodeJS environment. It solves some of the problems of nodeJS code deployment and connects various published modules. NPM serves as a management platform for the management and distribution of third-party packages, since the specifications and modules written by various developers are scattered, which is difficult for users to understand. NPM allows users to download and install third-party packages or command lines written by others, and distribute their own packages or command lines on the NPM platform for others to use.

In my opinion, an NPM package also has a life cycle, from the development, release, installation, update, uninstall/deprecation phases, which see an NPM plug-in from birth to death.

2. Package and package description file

NPM involves two main bodies: packages (node_modules) and package description files (package.json).

  • package.json: Manages the file behavior of all third-party plug-ins and packages in the current project
  • node_modules: Used to store quiltnodejstherequireAny files and folders to load

In addition, based on the above, we can see the relationship between package and Module:

  • moduleIs not necessarilypackage(e.g.,nodeBuilt-in modules),packageIt must bemodule
  • containingpackage.jsonOf the filemoduleIt must bepackage
{
  "name": "jctalk"./ / package name
  "version": "1.0.1"./ / version number
  "description": "a hello jctalk".// Package description
  "main": "./jctalk.js".// Import file
  "scripts": {// Script description object
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [// Keyword array
    "hello"."jctalk"]."author": "Jesse Liu"./ / the author
  "license": "MIT"// List of licenses used by the current package
}

Copy the code

3.npmPackage download, installation, uninstall, release and other functions

(1) Login or registrationnpmaccount

  • Method 1: You can go to the official website of NPM to register yourself. After registration, you need to verify the email before using it.

  • Method 2: Cli registration. To register an account, enter NPM adduser on the command line interface.

    $ npm adduser
    Username: jesseliu
    password:
    Email:jesseliu@gmail.com
    Copy the code

    Login Account:

    $ npm login
    Username: jesseliu
    Password:
    Email: (this IS public) [email protected]
    Logged in as jesseliu on https://registry.npmjs.org/.
    Copy the code

(2) NPM initializes the project

Initialize the package description file and fill it in question and answer mode through the command line.

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (jctalk) jctalk
Version: (1.0.1) 1.0.1git repository:
license: (MIT) MIT
About to write to D: \web\jctalk\package.json: {"name":"jctalk","version":" ", "1.0.1description":"a hello jctalk","main": "./jctalk.js","scripts": {"test":"echo \"Error: no test specified\" && exit1 "},"keywords": ["hello","jctalk"]."author":"Jesse Liu","license":"MIT"}Is this OK? (yes) y
Copy the code

(3) Upload the release package

First you need to write the package file jctalk.js to be published:

exports.jctalk = function(){
  return "hello jctalk";
}
Copy the code

Then enter the command to upload and publish:

$ npm publish . npm notice npm notice package: [email protected] NPM notice === Tarball Contents === NPM notice 57B jctalk.js NPM notice 275B package.json NPM notice 0 Readme. md NPM notice === Tarball Details === NPM notice name: JCTalk NPM notice Version: 1.0.1 NPM notice Size: 369 B npm notice unpacked size: 332 B npm notice shasum: 0e5bca0553d5c02c8f73a3263a17fab0fe702d22 npm notice integrity: sha512-njXdhYQMacX6f[...] SHnaVmJ3Q4htw== NPM notice Total Files: 3 NPM notice + [email protected]Copy the code

At this point, the NPM file will package the directory into an archive file and upload it to the official source repository.

Open the official website to find published packages.

(4) Installation package

To verify whether the package you uploaded is successful, you can change the directory to download and install the package.

$ npm install jctalk
npm WARN saveError ENOENT: no such file or directory, open 'D:\web\test_jctalk\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'D:\web\test_jctalk\package.json'
npm WARN test_jctalk No description
npm WARN test_jctalk No repository field.
npm WARN test_jctalk No README data
npm WARN test_jctalk No license field.

+ [email protected]
added 1 package from 1 contributor and audited 1 package in3.502 s found vulnerabilitiesCopy the code

(5) Analysis package

In the process of using the NPM package, you can use the command to resolve the package directory of the current project if you want to import the package you are using.

$NPM ls D:\web\test_jctalk '-- [email protected]Copy the code

(6) Update package

During a version iteration, the version number needs to be updated:

+ "version": "1.0.0"
- "version": "1.0.1",

Copy the code

(7) Cancel the release

During project development, you can use:

npm unpublish jstalk --force

Copy the code

4. Possible problems

Code E403: indicates that the NPM account is not registered or the email address has not been verified.

npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/jctalk - Forbidden
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy.
Copy the code

It may also happen when your source is set as a third party source, such as taobao source may cause the problem. Just change the source back to the default, as follows:

npm config set registry http://registry.npmjs.org

Copy the code

Code E402: indicates that private packages are to be published, but private packages need to be charged.

402 Payment Required - PUT 
http://registry.npmjs.org/@dinghx%2funifiedValidator 
- You must sign up for private packages

Copy the code

This is because the package name is @xxx/ XXXX. NPM will think that to publish private packages, private packages need to be charged, so it needs to change the command to:

npm publish --access public

Copy the code

The package name is similar to

npm ERR! Package name too similar to existing packages; 
try renaming your package to '@dinghx/captcha' and publishing 
with 'npm publish --access=public' instead : your-package

Copy the code

The mailbox is not verified.

npm ERR! you must verify your email before publishing a new package
: https://www.npmjs.com/email-edit : your-package

Copy the code

Just log in to the mailbox and verify.

Reference article:

1. Gold nuggets article: February jun, NPM “release their own NPM package, juejin.cn/post/687776…

2. Nuggets article: I am a tail, NPM release package tutorial, address: juejin.cn/post/684490…

The above is only personal learning record, if there is any mistake, please point out, thank you very much.