DEV Community

Cover image for I published a package on NPM for the first time - and here's how to do it yourself! (Via replit.com)
manu
manu

Posted on • Edited on

I published a package on NPM for the first time - and here's how to do it yourself! (Via replit.com)

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

  • You should see something like this:
    Image description

  • Now, click on the "Shell button"
    Image description

  • Run the following command:

npm init
Enter fullscreen mode Exit fullscreen mode

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: Image description
  • Fill in the version number: Image description
  • Fill in the description: Image description
  • Fill in the entry point (The main file) Image description
  • Fill in the test command, git repository, keywords, and author. These parameters are optional Image description
  • Type "Yes" Image description
  • You should now see another file Image description

Step 4 - Logging in to NPM

  • Type npm login in the shell
  • Enter your username and password Image description
  • Don't worry if you don't see anything when you type your password. This is for security purposes
  • Type your email Image description
  • You might have to verify your email Image description

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
Enter fullscreen mode Exit fullscreen mode

Step 6 - Let's publish our code

Run npm publish in the console
You should see something like this:
Image description

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))
Enter fullscreen mode Exit fullscreen mode

Demo

Top comments (0)