Skip to content

Instantly share code, notes, and snippets.

@ruvnet
Created April 10, 2025 14:25
Show Gist options
  • Save ruvnet/68cee7ebe970fdf75c26cf0562391638 to your computer and use it in GitHub Desktop.
Save ruvnet/68cee7ebe970fdf75c26cf0562391638 to your computer and use it in GitHub Desktop.
By following these steps, you can create powerful, reusable NPX components using Vite.js that others can easily execute without installation or use in their projects via npm install[2][4].

Creating Custom NPX Components with Vite.js

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.

Setting Up Your Project

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

Configuring Your Project for NPX

1. Create Your Component Structure

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

2. Configure Vite for Library Mode

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'
        }
      }
    }
  }
})

3. Add Bin Property to Package.json

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"
  }
}

4. Create an Executable Entry Point

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].

5. Generate TypeScript Declarations

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'] })
],

Building and Testing Locally

Build your component library:

npm run build

To test your NPX command locally before publishing:

npm link
npx my-npx-command

Publishing to NPM

1. Log in to NPM

Ensure you have an NPM account and log in via the terminal:

npm login

2. Prepare for Publishing

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]

3. Publish Your Package

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

Using Your NPX Component

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

Adding More Functionality

For more complex components, you might want to:

  1. Add command-line arguments parsing using libraries like commander or yargs[2]
  2. Include interactive prompts with inquirer
  3. Implement configurable options
  4. Add colorful output with libraries like chalk

Making Updates

When you want to update your component:

  1. Make your changes
  2. Update the version in package.json (following semantic versioning)
  3. Rebuild and republish:
    npm run build
    npm publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment