How to test node modules before publishing it to npm registtry

In this post , I will show you how to test npm modules before publishing it to npm registry

Let’s first create a module for converting string to titlecase.
Run following command

npm init -y
touch index.js

After running the above command your folder strucure will look like below

Imgur

Open your project in VS Code and then edit the index.js file

module.exports=function(str){
    return str[0].toUpperCase()+str.slice(1);
}

How to test the module localy

To test the module locally we will use npm link command which create a symoblic link .
To create link run the following command

cd titlecase
npm link

if command run successfully you will see the following output

C:\Users\lenovo\AppData\Roaming\npm\node_modules\titlecase -> F:\blog\modules\titlecase

How to use

To use the above module in your main project. Follow the following steps

  • Navigate to your main project
  • Run the following command in the terminal (make sure your are inside the project)
     npm link titlecase
    
    if command run successfully you will see output as below
    F:\blog\modules\module-demo\node_modules\titlecase -> C:\Users\lenovo\AppData\Roaming\npm\node_modules\titlecase -> F:\blog\modules\titlecase
    

Now you can use the module as below

const titleCase=require('titlecase');
console.log(titleCase("hello"));
Reactions

Post a Comment

0 Comments

Close Menu