Publishing a Node.js Packaged Module to the NPM Registry

In the previous section, you created a local Node.js Packaged Module using the npm pack command. You can also publish that same module to the NPM registry at http://npmjs.org.

When modules are published to the NPM registry, they are accessible to everyone, via the npm utility discussed earlier. The registry therefore allows you to distribute your modules and applications to others more easily.

The following steps describe the process of publishing a module to the NPM registry. These steps assume that you have completed steps 1–5 from the previous section:

1. Create a public repository to contain the code for the module. Then push the contents of the .../censorify folder up to that location. In this case, I created a GitHub repository at the following location: https://github.com/bwdayley/nodebook/tree/master/ch03/censorify.

2. Create an account at https://npmjs.org/signup.

3. Use the following command from a command prompt to add the user you created to the environment:

npm adduser

4. Type in the username, password, and email that you used to create the account in step 2.

5. Modify the package.json file to include the new repository information and any keywords that you want made available in the registry search, as shown in lines 7–14 in Listing 3.3:

Listing 3.3 package.json: Package definition that defines the Node.js module that includes the repository and keywords information


01 {
02   "author": "Brad Dayley",
03   "name": "censorify",
04   "version": "0.1.1",
05   "description": "Censors words out of text",
06   "main": "censortext",
07  "repository": {
08    "type": "git",
09    "url": "http://github.com/bwdayley/nodebook/tree/master/ch03/censorify"
10   },
11   "keywords": [
12     "censor",
13     "words"
14  ],
15   "dependencies": {},
16   "engines": {
17       "node": "*"
18   }
19 }


6. Publish the module using the following command from the .../censor folder in the console:

npm publish

Once the package has been published, you can search for it on the NPM registry and use the npm install command to install it into your environment.

To remove a package from the registry, make sure that you have added to the environment a user with rights to the module, using npm adduser, and execute the following command:

npm unpublish <project name>

For example, the following command unpublishes the censorify module:

npm unpublish censorify

In some instances, you will not be able to unpublish the module without using the --force option, which forces the removal and deletion of the module from the registry. Here’s an example:

npm unpublish censorify --force

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.224.135