Use npm to publish the Node. JS package tutorial, and npm to publish the node. js tutorial
Npm is the package manager of Node. JS. During Node. JS development, it is often used to install/uninstall the package. In fact, the work of releasing a package is also done by it.
Configure package. json
To package a program, you must configure various settings, which are specified by package. json in the root directory of the program package. The content of package. json must be in strict JSON format, that is:
1. The string must be enclosed in double quotation marks rather than single quotation marks;
2. Attribute names must be enclosed in double quotation marks;
3. Do not add a comma after the last attribute.
There are many attributes of the configuration object. For details, refer to the following common items:
1. name: the package name. It cannot be the same as the existing package.
2. version: version number.
3. description: a brief introduction.
4. author: author information. Includes three attributes: name, email, and url.
5. bin: if the program contains executable files (mainly called in the command line), you can specify multiple files here.
6. main: The program entry for calling this package using require.
7. dependencies: the dependent package. You can specify the version number.
After package. json is configured, You can package and install package locally to test whether the program works properly. The installation command is:
Copy codeThe Code is as follows:
Npm install <local path>
In addition, you should note that if you want the executable programs in the package to run in the Node. JS environment, add the following line at the beginning of the program entry file:
Copy codeThe Code is as follows:
#! /Usr/bin/env node
Without this line, it will be opened by default, instead of running in the Node. JS environment.
Register an npm account
To publish the package to npm, you must register an account first. Npm does not provide a webpage registration wizard. The registration should also be done through the command line:
Copy codeThe Code is as follows:
Npm adduser
After the command is executed, a prompt is displayed, indicating the user name, Email, and password. After the command is entered, wait for a while.
Release package
After the preparation is complete, you can run the following command to release the package:
Copy codeThe Code is as follows:
Npm publish <local path>
To update the package, you only need to modify the version number in package. json and re-execute the release command.