Create and publish a NPM package

May 12, 2024• Edited: June 13, 2024

With npm publish you can publish a NPM package, to do that you need to be logged, you can do it by using npm login.

Edit: Scoped packages are private by default, so you need to use the —access public flag to make it public. Non-public packages require payment.

You can initialize the package with npm init --scope=@username or add your username in the package.json to publish with your user scope.

{
  "name": "@username/package-name",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "test"
  }
}

When you make changes, update the version number in the package.json, according to semantic versioning .

  • Patch: 1.0.1 for bug fixes
  • Minor: 1.1.0 for new features that are backwards-compatible
  • Major: 2.0.0 for changes that break backwards compatibility

Also you can use file like .npmignore if there are files you don’t want to publish, similar to .gitignore.

# .npmignore
/src
tsconfig.json
jest.config.js