Home >Web Front-end >JS Tutorial >Tutorial on publishing Node.JS packages using npm_node.js
npm is the package manager for Node.JS. When doing Node.JS development, it is often used to install/uninstall packages. In fact, it does the work of publishing packages.
Configuration package.json
To package a program, you must first configure various settings, which are specified by package.json in the root directory of the package. The content of package.json must be in strict JSON format, that is:
1. Strings must be enclosed in double quotes, not single quotes;
2. Attribute names must be enclosed in double quotes;
3. Never add an extra comma after the last attribute.
There are many attributes of the configuration object. For details, you can refer to here. Here are the commonly used items:
1.name: Package name, cannot be the same as an existing package.
2.version: version number.
3.description: a brief introduction.
4.author: author information. Contains three attributes: name, email, and url.
5.bin: If there is an executable file in the program (mainly called from the command line), specify it here. You can specify multiple files.
6.main: The program entry when calling this package using require.
7.dependencies: Dependent packages, you can specify the version number.
After configuring package.json, you can package and install it locally first to test whether the program operates normally. The installation command is:
Register npm account
To publish the package to npm, you need to register an account first. npm does not provide a web version of the registration wizard. Registration also needs to be done through the command line:
Publish package
The preparations are all done. Execute the following command to publish the package: