A guide on how to create an npm package, publish it to the internet, and make it downloadable and installable via “npm install” or “npm install —global”.
Before publishing an npm package, make sure your Node.js program is complete and that you have created a package.json.
First, log in to npm:
npm login
If you don’t have an npm account, create one from the following:
Publish the program in the current directory as an npm package with the following command:
npm publish
To check if the npm package has been published successfully, use the following command:
npm show <package-name>
As an example, for the npm package I created, it displays as follows:
$ npm show notification-cli
[email protected] | BSD-3-Clause | deps: 4 | versions: 5
Minimalistic Command Line Notification Application under 50 Lines
https://github.com/yuis-ice/notification-cli
keywords: notifications, nodejs, cli, reminder, command-line, scheduler, cli-app, command-line-tool, time-management
bin: noc
dist
.tarball: https://registry.npmjs.org/notification-cli/-/notification-cli-1.0.4.tgz
.shasum: 2480a8ea01264fb3ceebe85563c7da7faa572541
.integrity: sha512-rr2nsXMp25wFpVQ..qECk3o5lJQu8lw==
.unpackedSize: 7.4 kB
dependencies:
commander: ^6.2.1 moment: ^2.29.1 node-notifier: ^9.0.0 node-schedule: ^2.0.0
maintainers:
- yuis-ice <***>
dist-tags:
latest: 1.0.4
published 4 days ago by yuis-ice <***>
(Reference)
npm packages not showing up - Google Search node.js - I published one package on npm, but it’s not showing in the search list when i am going to search - Stack Overflow
That’s how to publish an npm package, but I’ll leave my package.json as an example below. You are free to use and copy it under the license. Please use it as a reference. Also, I’ll explain each property below.
{
"name": "notification-cli",
"version": "1.0.4",
"description": "Minimalistic Command Line Notification Application under 50 Lines",
"main": "noc",
"bin": {
"noc": "./noc"
},
"dependencies": {
"commander": "^6.2.1",
"moment": "^2.29.1",
"node-notifier": "^9.0.0",
"node-schedule": "^2.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"homepage": "https://github.com/yuis-ice/notification-cli",
"repository": {
"type": "git",
"url": "https://github.com/yuis-ice/notification-cli.git"
},
"keywords": [
"notifications",
"nodejs",
"cli",
"reminder",
"command-line",
"scheduler",
"cli-app",
"command-line-tool",
"time-management"
],
"author": "*** <***>",
"license": "BSD-3-Clause"
}
// description
The description property is displayed on the npm website. It has a slight SEO effect. If you have time, set it.
// bin
The bin property is data referenced when npm is installed as a global package. It specifies the executable file.
If you don’t specify this, even if you execute the npm install —global command and install the package, the command won’t execute. (Because it’s not in the npm global package path)
"bin": {
"noc": "./noc"
},
// homepage // repository // keywords
These properties are displayed on the npm website. They have an SEO effect. If you want to send link sources to a GitHub page, don’t forget the “repository” property. “keywords” has a reasonable SEO effect in searches on the npm website.
This allows you to install the published npm package globally and make it executable as a command with the following command:
npm i --global notification-cli
(My created npm package and package.json: notification-cli)
yuis-ice/notification-cli: Minimalistic Command Line Notification Application under 50 Lines notification-cli/package.json at main · yuis-ice/notification-cli
(Other potentially helpful npm packages and package.json)
tldr-node-client/package.json at master · tldr-pages/tldr-node-client node-sleep/package.json at master · erikdubbelboer/node-sleep