Publishing a Package

Learn how to publish a package to npm.

Create an account

First, we will need an npm account. If you don’t have one yet, you can create it here.

CLI login

Next, we need to log in from the npm CLI. It is installed alongside Node.js. Use npm login and fill in your details.

root@educative:/# npm login
Username: zorro
Password: 
Email: (this IS public) zorro@educative.io
Logged in as zorro on https://registry.npmjs.org/.
root@educative:/# 

The folder

If you have already made a package, navigate to that folder. If you are just getting started, make a new directory and navigate into it.

root@educative:/# mkdir my_first_package
root@educative:/# cd my_first_package

You can name your folder whatever you like.

npm init

Once you are in the root folder of your package, run the npm init command. This command will ask you for some input and set up your package.json file. Let’s see how it looks.

This utility will walk you through creating a package.json file.
package name: (my_test_package) my-super-awesome-package
version: (1.0.0) 
description: This is my first package. It's pretty cool.
entry point: (index.js) 
test command: 
git repository: 
keywords: cool
author: zorro
license: (ISC) 
About to write to /my_test_package/package.json:

{
  "name": "my-super-awesome-package",
  "version": "1.0.0",
  "description": "This is my first package. It's pretty cool.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "cool"
  ],
  "author": "zorro",
  "license": "ISC"
}


Is this OK? (yes) yes

The values in the parentheses, (), represent the default values that will be used if you press enter. You can skip items that you do not know about.

npm publish

The final step to publish your package to the registry. This can be done with the npm publish command.

npm publish
npm notice 
npm notice 📦  my-super-awesome-package@1.0.0
npm notice === Tarball Contents === 
npm notice 300B package.json
npm notice === Tarball Details === 
npm notice name:          my-super-awesome-package                
npm notice version:       1.0.0                                   
npm notice package size:  297 B                                   
npm notice unpacked size: 300 B                                   
npm notice shasum:        1fc163c8b09e3df8e6fe57bac350ab5f4508f6df
npm notice integrity:     sha512-4o14PvienJY5Z[...]YyUj5mrWsTKLw==
npm notice total files:   1                                       
npm notice 
+ my-super-awesome-package@1.0.0

Congratulations! Your package is now on npm!

Get hands-on with 1200+ tech skills courses.