CodeBlanks Logo Icon CodeBlanks

npm publish

Learn to publish packages with npm publish.

npm publish

npm publish uploads your package to the npm registry. Once published, anyone in the world can download it with npm install.

Before publishing you must log in with npm login. The name and version fields in package.json are required, and version must be unique for each publish. A second publish with the same name and version is rejected. To publish a new version, first bump it with npm version patch (or minor/major), then run npm publish. This way your package is updated over time and users get the new versions.

npm version

The npm version command bumps the version in package.json and creates a git tag. npm version patch bumps the patch number (for example 1.2.3 to 1.2.4), npm version minor bumps the minor number (1.2.3 to 1.3.0), and npm version major bumps the major number (1.2.3 to 2.0.0).

This command is the standard way to update the version before publishing. Thanks to the tag, every version is marked in git and easy to find when rolling back. After running npm version, you upload the new version with npm publish. This keeps version management standard and traceable.

Practice Print in name@version format

Define a string named content with the value {"name":"my-lib","version":"1.2.3"}. Parse it into an object with JSON.parse. Print pkg.name + "@" + pkg.version with console.log.

Starter code

index.js
// Define the package.json content (with name and version)
// Parse it into an object with JSON.parse
// Print in name@version format
// write here

Sample solution

index.js
const content = '{"name":"my-lib","version":"1.2.3"}';
const pkg = JSON.parse(content);
console.log(pkg.name + "@" + pkg.version);

Expected output

my-lib@1.2.3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the publish command

Question
npm ____
Answer
npm publish

Explanation: npm publish uploads the package to the registry.

2 What must be unique when running npm publish?

index.js
npm publish
  • The package description
  • The package name and version Correct answer
  • The author name
  • The license

Explanation: A package with the same name and version cannot be published twice.

3 Match each command to its action

Term Meaning
npm publish Uploads the package
npm login Logs into the account
npm version Bumps the version
npm whoami Returns the username

All lessons in Intermediate