Packages and modules
Contributing packages to the registry
Creating a package.json fileCreating Node.js modulesAbout package README filesCreating and publishing unscoped public packagesCreating and publishing scoped public packagesCreating and publishing private packagesPackage name guidelinesSpecifying dependencies and devDependencies in a package.json fileAbout semantic versioningAdding dist-tags to packages
Creating Node.js modules
Table of contents
Node.js modules are a type of package that can be published to npm.
Overview
- Create a 
package.jsonfile - Create the file that will be loaded when your module is required by another application
 - Test your module
 
Create a package.json file
- To create a 
package.jsonfile, on the command line, in the root directory of your Node.js module, runnpm init:- For scoped modules, run 
npm init --scope=@scope-name - For unscoped modules, run 
npm init 
 - For scoped modules, run 
 - Provide responses for the required fields (
nameandversion), as well as themainfield:name: The name of your module.version: The initial module version. We recommend following semantic versioning guidelines and starting with1.0.0.
 
For more information on package.json files, see "Creating a package.json file".
Create the file that will be loaded when your module is required by another application
In the file, add a function as a property of the exports object. This will make the function available to other code:
exports.printMsg = function() {console.log("This is a message from the demo package");}
Test your module
Publish your package to npm:
- For private packages and unscoped packages, use 
npm publish. - For scoped public packages, use 
npm publish --access public 
- For private packages and unscoped packages, use 
 On the command line, create a new test directory outside of your project directory.
mkdir test-directorySwitch to the new directory:
cd /path/to/test-directoryIn the test directory, install your module:
npm install <your-module-name>In the test directory, create a
test.jsfile which requires your module and calls your module as a method.On the command line, run
node test.js. The message sent to the console.log should appear.