censorify_daviddemo v0.1.1
How To CREATE, PUBLISH, And USE a Basic Node Package Module
CREATE
The following steps take you through the process of building a Node.js Packaged Mdoule using an example called censorify. The censorify module accepts text and then replaces certain words with asterisks:
Create a project folder named .../censorify
Inside, create a file named censortext.js
Copie this code inside censortext.js
var censoredWords = [
"sad",
"bad",
"mad"
];
var customCensoredWords = [];
function censor ( inStr ) {
for ( idx in censoredWords ) {
inStr = inStr.replace( censoredWords[idx], "****" );
}
for ( idx in customCensoredWords ) {
inStr = inStr.replace( customCensoredWords[idx], "****" );
}
return inStr;
}
function addCensoredWord ( word ) {
customCensoredWords.push( word );
}
function getCensoredWords () {
return censordWords.concat( customCensoredWords );
}
exports.censor = censor;
exports.addCensoredWord = addCensoredWord;
exports.getCensoredWords = getCensoredWords;
Create a package.json file, then define it.
Create a file README.md in the .../censorify folder. (Instruction to the module)
Navigate to the .../censorify folder in a console window and run the
npm pack
command to build a local package moduleThe
npm pack
command creates a censorify-0.1.1.tgz file in the .../censorify folder. This is your first Node.js Packaged Module.
PUBLIHING
The following steps describe the process of publishing the module to the NPM registry. These steps assume that you have completed steps 1 through 5 from the previous section:
- Create a public repository to contain the code for the module. Then push the content of the .../censorify folder up to that location. The following is an example of a Github repository URL:
https://github.com/username/projectname/directoryName/Censorify
Create an account at https://npmjs.org/signup
Use
npm adduser
command from a console prompt to add the user you created to the environmentType in the username, password and email that you used to create the account in step 2
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:
{
"author": "David-Alexandre",
"name": "censorify",
"version": "0.1.1",
"description": "Censors words out of text",
"main": "censortext",
"repository": {
"type": "git",
"url": "git@gitlab.com:EchoAppleTree/node-mongodb-angular-learn.git"
},
"keywords": [
"censor",
"words"
],
"dependencies": {},
"engines": {
"node": "*"
}
}
- Publish the module using the following command from the .../censorify 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 addes a user with rights to the module to the envrironment using npm adduser
and then execute the following command: npm unpublish <project name>
For example, the following command unpublished the censorify module: npm unpublished censorify
In some instances you cannot unpublished the module without using the --force
option. This option forces the removal and deletion of the module from the registry. For example: npm unpublish censorify --force
7 years ago