Home > Web Front-end > JS Tutorial > Tutorial on publishing Node.JS packages using npm_node.js

Tutorial on publishing Node.JS packages using npm_node.js

WBOY
Release: 2016-05-16 16:11:41
Original
1734 people have browsed it

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:

Copy code The code is as follows:

npm install

In addition, there is an unspoken rule to note. If you want the executable program in the package to run in the Node.JS environment, then please add this line at the front of the program entry file:
Copy code The code is as follows:

#!/usr/bin/env node

Without this line, it will open in the system default mode instead of running in the 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 also needs to be done through the command line:

Copy code The code is as follows:

npm adduser

After executing this command, prompts for entering your username, email, and password will appear in sequence. Just wait for a while after entering them.

Publish package

The preparations are all done. Execute the following command to publish the package:

Copy code The code is as follows:

npm publish

If you want to update the package, just modify the version number in package.json and re-execute the publish command.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template