Back to KB
Difficulty
Intermediate
Read Time
5 min
I Published My First npm Package β Here's Everything I Wish I Knew
By Codcompass TeamΒ·Β·5 min read
Publishing to npm is easy. Doing it RIGHT takes some planning. Here's my complete checklist.
Before You Start
Is It Worth Publishing?
Don't publish if:
- It's specific to one project
- The name is already taken (obviously)
- You're not willing to maintain it
Do publish if:
- You've copied this code between 3+ projects
- Others would benefit from it
- You're willing to fix bugs and accept PRs
Step 1: Project Setup
mkdir my-awesome-package && cd my-awesome-package
npm init -y
# Install dev dependencies
npm install -D typescript @types/node jest ts-jest
# Create source structure
mkdir src tests
Enter fullscreen mode Exit fullscreen mode
// package.json β the important fields
{
"name": "@armorbreak/my-awesome-package",
"version": "1.0.0",
"description": "A brief description of what your package does",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"files": ["dist", "README.md", "LICENSE"],
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"test": "jest",
"prepublishOnly": "npm run build && npm test"
},
"keywords": ["utility", "nodejs"],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/armorbreak001/my-awesome-package.git"
},
"bugs": {
"url": "https://github.com/armorbreak001/my-awesome-package/issues"
},
"homepage": "https://github.com/armorbreak001/my-awesome-package#readme",
"engines": {
"node": ">=16.0.0"
},
"publishConfig": {
"access": "public" // Required for scoped packages (@scope/name)
}
}
Enter fullscreen mode Exit fullscreen mode
Key Fields Explained
Field
Why It Matters
main
Entry point for CommonJS (require())
types
TypeScript type definitions
exports
Modern way to define entry points (Node 12+)
files
Controls what gets published (don't publish tests!)
engines
Prevents installation on incompatible Node versions
publishConfig.access
Required for @scoped packages
Step 2: Write the Code
// src/index.ts
export interf
π Mid-Year Sale β Unlock Full Article
Base plan from just $4.99/mo or $49/yr
Sign in to read the full article and unlock all 635+ tutorials.
Sign In / Register β Start Free Trial7-day free trial Β· Cancel anytime Β· 30-day money-back
