Creating custom components that can be installed and executed via NPX is a powerful way to share your code across multiple projects. With Vite.js, this process becomes more streamlined. Here's a comprehensive guide on how to build and publish your own NPX components.
First, you need to create a new Vite project:
npm create vite@latest my-npx-component
When prompted, select your preferred framework (React, Vue, etc.) and TypeScript for type safety[1].
Once the project is created:
cd my-npx-component
npm install
Create a lib
directory in your project root to house your component code. This separates your library code from the demo/development environment[1].
π my-npx-component
β£ π lib
β β π main.ts
β£ π src
β ... other files
Modify your vite.config.ts
file to use Vite's library mode[1]:
import { defineConfig } from 'vite'
import { resolve } from 'path'
import react from '@vitejs/plugin-react' // or vue if using Vue
export default defineConfig({
plugins: [react()],
build: {
lib: {
entry: resolve(__dirname, 'lib/main.ts'),
name: 'MyNpxComponent',
fileName: (format) => `my-npx-component.${format}.js`
},
rollupOptions: {
external: ['react'], // List external dependencies
output: {
globals: {
react: 'React'
}
}
}
}
})
To make your package executable with NPX, add a bin
property to your package.json
[5]:
{
"name": "my-npx-component",
"version": "1.0.0",
"description": "A custom NPX component",
"main": "dist/my-npx-component.umd.js",
"module": "dist/my-npx-component.es.js",
"types": "dist/main.d.ts",
"bin": {
"my-npx-command": "dist/bin.js"
},
"files": [
"dist"
],
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
}
}
Create a bin.ts
file in your lib
directory:
#!/usr/bin/env node
// Your executable code here
console.log('Hello from my NPX component!');
Make sure to transpile this file to JavaScript in your build process. The shebang (#!/usr/bin/env node
) is essential for NPX to execute your script properly[2][5].
To include TypeScript declarations in your package, install and configure the vite-plugin-dts
plugin[1]:
npm i vite-plugin-dts -D
Then update your Vite config:
import dts from 'vite-plugin-dts'
// In your defineConfig:
plugins: [
react(),
dts({ include: ['lib'] })
],
Build your component library:
npm run build
To test your NPX command locally before publishing:
npm link
npx my-npx-command
Ensure you have an NPM account and log in via the terminal:
npm login
Verify your package.json
has the correct metadata:
- Unique name (consider using a scoped name like
@username/my-npx-component
) - Version number
- Description
- Keywords for discoverability
- License information[4]
Run the publish command:
npm publish
For scoped packages, you may need to add --access public
if it's your first publication:
npm publish --access public
Once published, anyone can use your component without installation:
npx my-npx-command
Or install it globally:
npm install -g my-npx-component
my-npx-command
For more complex components, you might want to:
- Add command-line arguments parsing using libraries like
commander
oryargs
[2] - Include interactive prompts with
inquirer
- Implement configurable options
- Add colorful output with libraries like
chalk
When you want to update your component:
- Make your changes
- Update the version in
package.json
(following semantic versioning) - Rebuild and republish:
npm run build npm publish