-
Notifications
You must be signed in to change notification settings - Fork 2
Create NPM Module
An NPM module is a set of Javascript functionality bundled into a distributable package. NPM maintains the registry of all available packages, and is also the tool used to install packages from its registry.
The beauty of NPM is that you can assemble packages other people have created to create something new, and someone later on could use the package you create. If you have some code that you want to share with the world, publishing a module to NPM is easy.
I created this simple utility as index.js
:
var time = new Date().toTimeString();
console.log(time);
The next step is to create the information to publish with your package, stored in package.json
. NPM provides a wizard to make creating this file easy.
Simply run npm init
and answer the questions.
Your package.json
should look something like this:
{
"name": "whattimeisit",
"version": "1.0.0",
"description": "accurate time retrieval",
"main": "index.js",
"author": "HoursAndMinutes",
"license": "ISC"
}
Don't forget to include the GitHub repository link if the code is on GitHub!
In order to publish a package to NPM, you need to have an account registered. To do that, run npm adduser
. You can verify the account currently in use with npm config ls
.
Before you publish your package to NPM, check if the directory contains any files you do not want made public (e.g. passwords, private keys etc). You can add these to a file named .npmignore
to exclude them from package publishing. NPM will also honor .gitignore
if you have that.
Once you have finalized your package contents, run npm publish
. You can confirm the details of your package at https://npmjs.com/package/yourpackagename.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links