bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Node.js/Module Basics
Node.js•Module Basics

Node.js Publish a Package

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Node.js Publish a Package?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ my-package
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Package.json Essentials
Initialize Package
What Does it Mean to Publish a Package?

What Does it Mean to Publish a Package?

Publishing a package means making your Node.js module or project available for others to install and use via the npm registry.

This is how open-source libraries and tools are shared with the Node.js community.

When you publish a package, it becomes available for anyone to install using npm install your-package-name .

Note

Make sure your package provides value, and that it is not a duplicate of an existing package on NPM.

Initialize Package

Create a new directory and initialize your package:

mkdir my-package
cd my-package
npm init -y

Essential Files

A package should include these key files:

  • package.json - Metadata about your package
  • README.md - Documentation (supports Markdown)
  • index.js - Main entry point (or specify in package.json)
  • LICENSE - Terms of use (MIT, ISC, etc.)
  • .gitignore - To exclude node_modules, logs, etc.
  • .npmignore - Optional, to exclude files from the published package

Package.json Essentials

Ensure your package.json has these minimum fields:

{
 "name": "your-package-name",
 "version": "1.0.0",
 "description": "A brief description of your package",
 "main": "index.js",
 "scripts": {
 "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": ["keyword1", "keyword2"],
 "author": "Your Name <your.email@example.com>",
 "license": "MIT"
}

Verify Your Email

Check your email and verify your account before publishing.

Login via CLI

Open your terminal and run

npm login

You'll be prompted for

  • Username
  • Password
  • Email (must match your npm account)
  • One-time password (if you have 2FA enabled)

Check Login Status

npm whoami

Check Name Availability

npm view <package-name>

If the package with that name does not already exist, you can use that name.

If it does, you'll need to choose a different name in your package.json .

Test Package Locally

Before publishing, test your package locally:

# In your package directory
npm link
# In another project directory
npm link <package-name>

Publish to npm Registry

# First, make sure you're in the right directory
cd path/to/your/package
# Publish to the public npm registry
npm publish

Publish with a Specific Tag

npm publish --tag beta

Publish a Public Package (if using npm paid account)

npm publish --access public

Update the Version Number

Use semantic versioning (SemVer) to update your package version:

# For a patch release (bug fixes)
npm version patch
# For a minor release (backward-compatible features)
npm version minor
# For a major release (breaking changes)
npm version major

Update Changelog

Update your CHANGELOG.md to document the changes in this version.

Publish the Update

npm publish

Tag the Release (Optional)

If you're using Git, create a tag for the release:

git tag -a v1.0.0 -m "Initial release"
git push origin v1.0.0

Unpublishing a Package

To remove a package from the npm registry:

# Unpublish a specific version
npm unpublish <package-name>@<version>
# Unpublish the entire package (only works within 72 hours of publishing)
npm unpublish <package-name> --force

Important: Unpublishing is strongly discouraged as it can break other projects that depend on your package. Instead, consider using npm deprecate .

Deprecating a Package

If you want to prevent users from installing a version but keep it available for existing users:

# Deprecate a specific version
npm deprecate <package-name>@<version> "message"
# Example
npx deprecate my-package@1.0.0 "This version is no longer maintained. Please upgrade to v2.0.0"

Transferring Ownership

To transfer a package to another user or organization:

npm owner add <username> <package-name>

Best Practices

  • Follow Semantic Versioning - Use MAJOR.MINOR.PATCH version numbers appropriately
  • Write Good Documentation - Include clear usage examples in your README
  • Add Tests - Include unit tests and document how to run them
  • Use .npmignore - Only publish necessary files
  • Add Keywords - Help others discover your package
  • Choose the Right License - Make your terms clear to users
  • Maintain a Changelog - Document changes between versions
  • Use Continuous Integration - Automate testing and publishing

Summary

Publishing packages to npm is a great way to share your code with the Node.js community.

If you follow best practices and maintain your packages well, you can contribute valuable tools that others can build upon.

Remember

With great power comes great responsibility. When you publish a package, you're making a commitment to maintain it or clearly communicate its status to users.

Previous

Node.js Managing Dependencies

Next chapter

Core Modules

Start with Node.js HTTP Module