1. Download & Install Node.js
Download and Install Node.js by choosing your OS from the list here:
NOTE: Anywhere you see
MyCoolModule - Replace it with your own module name.
2. Create a Node Project
Create an empty project
Execute the following command:
Provide responses for the required fields (name and version), as well as the main field:
name: The name of your module.
version: The initial module version. We recommend following semantic versioning guidelines and starting with 1.0.0.
main: The name of the file that will be loaded when your module is required by another application. The default name is index.js.
Just press ‘Enter’ to the other questions…
3. Write your Module
There should now be a
package.json file inside your project directory.
We need to write our code to upload it as a module.
Note: In this example, we are simply writing a function to print some text to the console.
Create a file and name it
index.js in the project directory
Copy and paste the following code to index.js:
4. Publish Module to NPM
If you don’t have an npm account - Create one below:
Login using your credentials:
After logging in, you’re ready to publish!
5. Test your Module
Create another directory
Switch into the directory
Create your test script - I called mine
Create the test node project
Install your created module
Execute the test script
You Are Done!
Thanks for reading! I hope you enjoyed the speed-run through setting up a node module using
npm. Please don’t forget to share this article. We’ll be back again soon with more Node.js content!