Creating an NPM module

How to

  1. Create a repo that can be published as a public (unscoped) NPM module
  2. Register an account on npmjs.com
  3. Publish the module to NPM
  4. Add the dependency to a project
  5. Make the repo available in the cloud (optional)

1. Create a project that can be published as an NPM module

  1. Follow the instructions under "Create a package.json file" from the NPM docs
  2. Follow the instructions under "Create the file that will be loaded ..." from the NPM docs

2. Register an account on npmjs.com

  1. Go to npmjs.org > join and register an account
  2. login in the terminal:
$ npm login

3. Publish the module to NPM

$ npm publish

4. Add the dependency to a node project

Go to a separate project and add it with yarn or npm. Below we assume that the project was named npm-test-em:

$ yarn add npm-test-em

5. Make the repo available in the cloud

  1. Create a repo on github/gitlab or similar and follow the instructions to push your local project to the cloud
  2. Add the repo information to package.json:
// package.json
{
  // ...
  "repository": {
    "type": "git",
    "url": "https://gitlab.com/emlabs/npm-test-module.git"
  }
  // ...
}

Examples

References