-
Notifications
You must be signed in to change notification settings - Fork 1
NPM Configuration
- Sign up for a Github account
- Install Node.js
- Be sure you have the latest version of NPM
npm install npm@latest -g
npm set init.author.name "Edd Suárez"
npm set init.author.email "eddsuarez89@gmail.com"
npm set init.author.url "https://github.com/eddsuarez"
This way, when we run some npm commands later, it will already know who we are and will be able to autocomplete some information for us.
npm adduser
This next command will prompt you for an email and password, create or verify a user in the npm registry, and save the credentials to the~/.npmrcfile.
For our demo, we’ll create a simple npm module for generating random letters with specific length:
// index.js
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
function randomLetters(length) {
let random;
let output = '';
for (let i = 1; i <= length; i++) {
output += letters.substring(random = Math.floor(Math.random() * letters.length), random + 1);
}
return output;
};
module.exports = randomLetters;Create the README.md (markdown) file with details and usage examples for your users, this will be displayed on GitHub and npm.
Executing the following command will ask you a bunch of questions, and then write out a package.jsonfile. It is this file that effectively turns your code into a package.
npm init
If you want to namespace your modules, do this instead
npm init --scope=username
More details about of the contents of the package.json file can be found at https://docs.npmjs.com/files/package.json.
By adding the following (optional) to your package.json, you can specify the minimum Node version that your package requires to ensure your library will work for your users.
"engines": {
"node": ">=4.2.4"
},
Prior to publishing to npm, let’s first ensure that any changes have been committed to git and that everything has been pushed up to Github. It is also a good idea to create a version tag as well. Here’s how to do just that.
git add .
git commit -m “Initial release”
git tag v1.0.0
git push origin master --tags
Before publishing, you can install your module directly from github repository to be sure that your package install works correctly.
npm install git://github.com/eddsuarez/random-letters.git
npm install git://github.com/eddsuarez/random-letters.git#1.0.0
Or you can install your package from your package root directory, enter the following to install your package globally:
npm install . -g
To check if exists:
npm ls -g
Remember from earlier that we have already registered on npm, with thenpm addusercommand. With that, the actual publishing part is really easy.
npm publish
Afterwards, you’ll be able to install your package directly by name:
npm install random-letters
https://quickleft.com/blog/creating-and-publishing-a-node-js-module/