NPM is the Node.js Package Manager. It is often used to install/uninstall packages when developing Node.js. In fact, the job of publishing a package is also done by it.
Configure Package.json
To package the program, you first need to match the settings, which are specified by the Package.json in the program package root directory. The content of the Package.json must be in a strict JSON format, which means:
1. Strings should be enclosed in double quotes, but not in single quotes;
2. Attribute name must add double quotes;
3. Never add a comma after the last attribute.
Configuration objects have a lot of properties, you can see here, here is a list of commonly used items:
1.name: Package name, cannot repeat with existing package.
2.version: Version number.
3.description: A brief introduction.
4.author: Author information. Contains the name, email, url three property.
5.bin: If there is an executable file (mainly called in the command line) in the program, specify it here and you can specify more than one.
6.main: Use require to invoke the program entry when this package is invoked.
7.dependencies: The dependent package, you can specify the version number.
After the Package.json is configured, you can pack and install it locally once, and the test program works properly, and the installation commands are:
Copy Code code as follows:
NPM Install < local path >
In addition, there is a hidden rule to note that if you want the executable program in the package to run in a node.js environment, then add one line to the front of the program entry file:
Copy Code code as follows:
Without this line, it will open in the default mode of the system instead of running in a node.js environment.
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 is also done through the command line:
Copy Code code as follows:
After you execute this command, you will be prompted to enter your username, Email, password, and then wait a little while.
Publishing Packages
The preparation is done, and you can publish the package by executing the following command:
Copy Code code as follows:
NPM Publish < Local path >
If you want to update the package, just change the version number in Package.json, and then execute the release command again.