How to publish a npm package

April 2, 2024

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui(2024-04-02 23:06:26 +0000). How to publish a npm package. https://borui/blog/2024-04-02-en-how-to-publish-npm-package.
@misc{
  borui2024,
  author = {borui},
  title = {How to publish a npm package},
  year = {2024},
  publisher = {borui's blog},
  journal = {borui's blog},
  url={https://borui/blog/2024-04-02-en-how-to-publish-npm-package}
}

step1 Creating a package.json file

To do this, navigate to the root directory of your project and run the following command: npm init

This command will guide you to create a package.json file. You will get prompts to provide the following information:

package-name: As you learned earlier in this tutorial, the name of your package must be unique. Also it must be lowercase. It may include hyphens.

version: The initial value is 1.0.0. You update the number when you update your package using semantic versioning.

description: You can provide a description of your package here. Indicate what your package does and how to use it.

entry point: The entry file for your code. The default value is index.js.

test command: Here, you can add the command you want to run when a user runs npm run test.

git repository: The link to your remote repository on GitHub.

keywords: Add relevant keywords that will help others find your package on the NPM registry.

author: Add your name.

license: You can add a license or use the default license (Internet Systems Consortium (ISC) License). See the screenshot below for an example of how to answer the prompt questions:

An example of the created package.json:

{
  "name": "my_package",
  "description": "",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "types": "index.d.ts",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/monatheoctocat/my_package.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/monatheoctocat/my_package/issues"
  },
  "homepage": "https://github.com/monatheoctocat/my_package"
}
  • name: the current directory name
  • version: always 1.0.0
  • description: info from the README, or an empty string ""
  • scripts: by default creates an empty test script
  • keywords: empty
  • author: empty
  • license: ISC
  • bugs: information from the current directory, if present
  • homepage: information from the current directory, if present

step2 Test your module before publish

Install package into the project you want to test it in:

npm install /path/to/your/package/

This is better than pulish and install from npm, for you don't have to reinstall every time you make change.

step3 publish

To do this, navigate to the root directory of your project and run the following commands:

npm login
npm publish

reference

  1. Benjamin Semah. (FEBRUARY 1, 2023). How to Create and Publish an NPM Package – a Step-by-Step Guide. [Blog post]. freecodecamp. Retrieved from https://www.freecodecamp.org/news/how-to-create-and-publish-your-first-npm-package/

  2. 时光足迹. (8 Dec, 2021). 超详细 如何发布自己的 npm 包. 稀土掘金. [Blog post]. Retrieved February 1, 2024, from https://juejin.cn/post/7039140144250617887

  3. package.json. (March 26, 2024). npm docs. Retrieved April 2, 2024, from https://docs.npmjs.com/cli/v10/configuring-npm/package-json

  4. fancy. (Nov 11, 2011). you just provide one folder argument to npm install, argument should point toward the local folder instead of the package name: npm install /path. [Answer]. stackoverflow. https://stackoverflow.com/questions/8088795/installing-a-local-module-using-npm/8089029#8089029