How to Publish Your First NPM Package for AI Applications
Publishing an NPM package is one of the best ways to share your AI tools, build your reputation, and create passive income opportunities. This step-by-step guide walks you through the entire process.
Builder of AI agents, crypto trading bots, and open-source automation tools. Sharing practical guides on how to build, deploy, and profit from AI and DeFi technology.
Why Publish an NPM Package?
Publishing NPM packages has tangible benefits:
- Community โ Others use and improve your code
- Reputation โ A popular package is the best developer portfolio
- Income โ GitHub Sponsors, open-core monetization, consulting
- Learning โ The discipline of making something public forces quality
And in the AI space specifically, there's massive demand for well-built utility packages.
What Makes a Good AI NPM Package?
The best AI packages solve a specific, recurring problem:
- A unified API for multiple LLM providers
- A set of pre-built prompts for common tasks
- A connector for a specific data source (exchange, news API)
- A utility for parsing/validating LLM outputs
Bad idea: "A general AI library" (too broad, too much competition)
Good idea: "A TypeScript library for extracting structured data from crypto news articles" (specific, valuable)
Step 1: Plan Your Package
Before writing code, answer these questions:
- What problem does it solve?
- Who is the target user?
- What's the API surface (public functions/classes)?
- What's the package name? (Check npm.js first)
Step 2: Initialize Your Package
mkdir crypto-sentiment-ai && cd crypto-sentiment-ai
npm init -y
npm install typescript @types/node tsup --save-dev
npm install openai zod
Configure TypeScript:
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"declaration": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}
Configure build with tsup (much easier than raw tsc):
// package.json
{
"name": "crypto-sentiment-ai",
"version": "0.1.0",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"test": "node --experimental-vm-modules node_modules/.bin/jest"
}
}
Step 3: Write Your Core Code
// src/index.ts
import OpenAI from 'openai'
import { z } from 'zod'
const SentimentSchema = z.object({
sentiment: z.enum(['bullish', 'bearish', 'neutral']),
score: z.number().min(-1).max(1),
confidence: z.number().min(0).max(1),
reasoning: z.string(),
})
export type SentimentResult = z.infer<typeof SentimentSchema>
export interface SentimentAnalyzerOptions {
apiKey?: string
model?: string
}
export class CryptoSentimentAnalyzer {
private client: OpenAI
private model: string
constructor(options: SentimentAnalyzerOptions = {}) {
this.client = new OpenAI({ apiKey: options.apiKey || process.env.OPENAI_API_KEY })
this.model = options.model || 'gpt-4o-mini'
}
async analyze(text: string, asset: string): Promise<SentimentResult> {
const response = await this.client.chat.completions.create({
model: this.model,
messages: [{
role: 'user',
content: `Analyze sentiment about ${asset} in this text: "${text}"
Return JSON with: sentiment (bullish/bearish/neutral), score (-1 to 1), confidence (0 to 1), reasoning (string)`
}],
response_format: { type: 'json_object' }
})
const parsed = JSON.parse(response.choices[0].message.content!)
return SentimentSchema.parse(parsed)
}
async analyzeBatch(texts: Array<{ text: string; asset: string }>): Promise<SentimentResult[]> {
return Promise.all(texts.map(({ text, asset }) => this.analyze(text, asset)))
}
}
// Named exports for tree-shaking
export { CryptoSentimentAnalyzer as default }
Step 4: Write Tests
// src/__tests__/index.test.ts
import { CryptoSentimentAnalyzer } from '../index'
describe('CryptoSentimentAnalyzer', () => {
it('should return valid sentiment result', async () => {
const analyzer = new CryptoSentimentAnalyzer()
const result = await analyzer.analyze(
'Bitcoin ETF sees massive inflows as institutions pile in',
'Bitcoin'
)
expect(result.sentiment).toMatch(/bullish|bearish|neutral/)
expect(result.score).toBeGreaterThanOrEqual(-1)
expect(result.score).toBeLessThanOrEqual(1)
})
})
Step 5: Write an Excellent README
This is the most important step. Your README is your package's landing page.
Structure:
- One-line description โ What it does
- Install โ
npm install your-package - Quick start โ Working code example in 5 lines
- API reference โ All public methods documented
- Examples โ Real use cases
- Contributing โ How to submit PRs
- License โ MIT is standard for open-source
Step 6: Publish to NPM
# 1. Create an NPM account at npmjs.com
# 2. Login via CLI
npm login
# 3. Build
npm run build
# 4. Dry run first (see what would be published)
npm publish --dry-run
# 5. Publish!
npm publish --access public
Step 7: Grow Your Package
After publishing:
- Share it โ Post on Twitter/X, Reddit r/javascript, dev.to
- Add GitHub badges โ npm version, downloads, license
- Respond to issues โ Building trust and community
- Write examples โ Each example is a potential SEO landing page
- Set up GitHub Sponsors โ Let users support you financially
Monetization Strategies
- Open Core โ Free base, paid premium features
- GitHub Sponsors โ Community support
- Consulting โ Package users become consulting clients
- Courses โ "Build X with my package" courses
Popular crypto/AI NPM packages regularly receive $500โ$5,000/month in sponsorships once they hit ~1,000 weekly downloads.
Get Inspired by Our Packages
Check the Tools page to see our published NPM packages โ all open-source with full source code on GitHub.
Related Articles
NPM Packages for AI Development: The Ultimate 2025 Guide
3 min read
Passive IncomeRecurring Revenue Models for Open Source Projects
4 min read
Passive IncomeAI Agent Monetization: 8 Ways to Make Money From Your Code
4 min read
AI AgentsTop 10 Open Source GitHub Repos Every AI Developer Should Fork
4 min read