npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently. ... Most commonly, it is used to publish, discover, install, and develop node programs.
Source: Google
Step 1
- Create an account at NPM, and verify your email
- Then, create an account at https://replit.com, and click the * "Create" button at the the top left corner of the page !
- Select "NodeJS" Image description
Step 2
npm init
This will help you generate a package.json
file. This will be useful later.
Step 3 - Generating a package.json file
- Fill in the name:
- Fill in the version number:
- Fill in the description:
- Fill in the entry point (The main file)
- Fill in the test command, git repository, keywords, and author. These parameters are optional
- Type "Yes"
- You should now see another file
Step 4 - Logging in to NPM
- Type
npm login
in the shell - Enter your username and password
- Don't worry if you don't see anything when you type your password. This is for security purposes
- Type your email
- You might have to verify your email
Step 5 - Let's write some code!
Type the following your code in the index.js
file
// Taken from StackOverflow for demo purposes
function _genId(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
module.exports = _genId
Step 6 - Let's publish our code
Run npm publish
in the console
You should see something like this:
Now, you can search your package on npm and install the npm package. Check out the example.js
in the demo code
const uuid = require("awesome-string-generator");
console.log(uuid(100))
Top comments (0)