How to publish a node. js package using NPM Heero.luo posted 3 year ago, viewed 5,065 times
NPM is the package Manager for node. js. When you are developing node. js, you often use it to install/uninstall packages. In fact, the work of releasing a package is also done by it.
Configure Package.json
To package a program, you first need to set up the settings, which are specified by Package.json in the program package root directory. The content of the Package.json must be in a strict JSON format , which means:
- strings are enclosed in double quotation marks , but not in single quotes;
- attribute names must be double-quoted ;
- Never add a comma after the last attribute.
There are many properties of the configuration object, see here for a list of commonly used items:
- Name: Package name, cannot be duplicated with existing packages.
- Version: Revision number.
- Description: A brief introduction.
- Author: author information. Contains the name, email, and URL three properties.
- Bin: If the program has an executable file (mainly called inside the command line), it is specified here, you can specify more than one.
- Main: The program entry when invoking this package with require.
- Dependencies: Dependent packages, you can specify a version number.
After the Package.json is configured, it can be packaged locally and installed once, the test program is working properly, the installation command is:
npm install <本地路径>
In addition, there is a unspoken rule to note that if you want the executable program in the package to run in node. js, add this line to the front of the program's entry file:
#!/usr/bin/env node
Without this line, it will open as a system default, rather than running in the context of node. js.
Sign up for NPM account
To publish the package to NPM, you need to register an account first. NPM does not provide the web version of the Registration Wizard. Registration is also done through the command line:
npm adduser
After executing this command, you will be prompted to enter the user name, Email, password, and then wait for a while.
Publishing Packages
The preparation is ready, and you can publish the package by executing the following command:
npm publish <本地路径>
If you want to update the package, just modify the version number in Package.json and then re-execute the Publish command.
How to publish a node. js package using NPM