Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
General / UtilityintermediateNew

NPM Publish

Share

Prepare and publish npm package

Works with OpenClaude

You are an npm package maintainer. The user wants to prepare a local npm package and publish it to the npm registry.

What to check first

  • Run npm whoami to verify you're logged into npm registry
  • Check package.json exists and has name, version, and description fields
  • Run npm list to ensure all dependencies are installed
  • Verify .npmignore or files field in package.json to control what gets published

Steps

  1. Update the version field in package.json using semantic versioning (e.g., 1.0.0, 1.2.3)
  2. Run npm test to ensure all tests pass before publishing
  3. Run npm run build if your package has a build step (check scripts.build in package.json)
  4. Run npm pack to create a tarball and preview exactly what will be published
  5. Review the tarball contents with tar -tzf <package-name>-<version>.tgz to confirm no sensitive files are included
  6. Run npm publish to push the package to the npm registry
  7. Verify publication with npm view <package-name>@<version> to confirm the version exists on npm
  8. Create a git tag matching the version: git tag v<version> and push with git push --tags

Code

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

function publishPackage() {
  const packageJsonPath = path.join(process.cwd(), 'package.json');
  
  // Read current package.json
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  console.log(`📦 Current version: ${packageJson.version}`);
  
  // Verify npm login
  try {
    execSync('npm whoami', { stdio: 'inherit' });
  } catch (error) {
    console.error('❌ Not logged into npm. Run: npm login');
    process.exit(1);
  }
  
  // Run tests
  console.log('\n🧪 Running tests...');
  try {
    execSync('npm test', { stdio: 'inherit' });
  } catch (error) {
    console.error('❌ Tests failed. Fix issues before publishing.');
    process.exit(1);
  }
  
  // Run build if script exists
  if (packageJson.scripts && packageJson.scripts.build) {
    console.log('\n🔨 Building package...');
    try {
      execSync('npm run build', { stdio: 'inherit' });
    } catch (error) {
      console.error('❌ Build failed.');
      process.exit(1);
    }
  }
  
  // Create tarball preview
  console.log('\n📋 Creating preview tar

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
generalnpmpublish

Install command:

curl -o ~/.claude/skills/npm-publish.md https://claude-skills-hub.vercel.app/skills/general/npm-publish.md

Related General / Utility Skills

Other Claude Code skills in the same category — free to download.

Want a General / Utility skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.