package.json
files, particularly their scripts
section, are incredibly versatile for automating development workflows. Here are some ideas for package.json
apps
(meaning, scripts that act like small applications or tools within your project):
-
"Pre-commit" and "Pre-push" Hooks:
precommit
: Run linters, formatters (e.g., Prettier), and unit tests before allowing a commit. This ensures code quality and consistency.prepush
: Run more extensive tests (e.g., integration tests), build steps, or security checks before pushing to the remote repository.
-
Smart Start Scripts:
start:dev
: Start a development server with hot-reloading and detailed logging.start:prod
: Build the application and then serve the production-ready build, simulating a production environment locally.start:all
: For full-stack applications, useconcurrently
ornpm-run-all
to start both the frontend and backend servers simultaneously.start:tunnel
: Start your dev server and immediately expose it to the internet via a tunneling service (e.g., ngrok, Cloudflare Tunnel) for easy sharing or webhook testing.
-
Dependency Management Helpers:
deps:check
: Runnpm outdated
or a similar tool to list outdated dependencies.deps:update
: Update minor/patch versions of dependencies (e.g.,npm update
).deps:audit
: Runnpm audit
and then provide instructions or automatically fix known vulnerabilities.deps:clean
: Removenode_modules
andpackage-lock.json
and then reinstall dependencies (rm -rf node_modules package-lock.json && npm install
).
-
Documentation & Information:
docs:generate
: Generate API documentation using tools like JSDoc, TypeDoc, or Swagger.docs:serve
: Serve the generated documentation locally.info:versions
: Display versions of key dependencies or the Node.js/npm version used in the project.info:scripts
: List all availablenpm
scripts with a brief description (you'd have to manually add descriptions using a custom format, aspackage.json
doesn't natively support script descriptions yet, but tools likenpm-script-info
can parse comments or conventions).
-
Clean-up & Maintenance:
clean
: Remove build artifacts, temporary files, cache directories, and log files.clean:modules
: Specifically removenode_modules
and potentiallypackage-lock.json
.cache:clear
: Clear build tool caches (e.g., Webpack, Babel).
-
Frontend Build & Optimization:
build:css
: Compile Sass/Less/PostCSS, autoprefix, and minify CSS.build:js
: Transpile, bundle, and minify JavaScript (e.g., using Webpack, Rollup, Parcel, esbuild).build:assets
: Optimize images, fonts, and other static assets.build:analyze
: Run a bundle analyzer to visualize the size of your JavaScript bundles.build:report
: Generate a detailed report of the build process, including file sizes, compilation times, etc.
-
Testing & Quality Assurance:
test:unit
: Run only unit tests.test:integration
: Run only integration tests.test:e2e
: Run end-to-end tests (e.g., Cypress, Playwright).test:watch
: Run tests in watch mode for continuous feedback.test:coverage
: Generate code coverage reports.lint:fix
: Run linter and automatically fix fixable issues.audit:a11y
: Run accessibility audits on your deployed or local application.
-
Deployment Helpers:
deploy:staging
: Build and deploy to a staging environment.deploy:prod
: Build and deploy to production (with appropriate safeguards, perhaps requiring manual confirmation).deploy:cdn
: Upload static assets to a CDN.deploy:rollback
: A script to facilitate rolling back to a previous deployment (requires external tools/setup).
-
Database Operations:
db:migrate:up
: Run database migrations.db:migrate:down
: Revert the last migration.db:seed
: Seed the database with initial data.db:reset
: Drop, create, migrate, and seed the database (useful for development).db:backup
: Create a backup of the database.db:restore
: Restore the database from a backup.
-
API & Server Management:
api:docs
: Generate API documentation (e.g., Swagger/OpenAPI).api:test
: Run API integration tests.server:start
: Start the backend server.server:restart
: Restart the backend server.server:logs
: Tail server logs (e.g.,tail -f logs/server.log
).
-
Containerization:
docker:build
: Build Docker images for your application.docker:run
: Run Docker containers (e.g., for local development environment).docker:compose:up
: Start services defined in adocker-compose.yml
file.docker:prune
: Clean up unused Docker images/containers.
-
Interactive Prompts:
- Use
inquirer.js
or similar to create interactivenpm run
scripts that prompt the user for input (e.g.,npm run create:component
asks for component name, type, and generates boilerplate).
- Use
-
Local Dev Environment Provisioning:
env:setup
: A script to check for necessary local tools (e.g., Docker, specific Node.js version, database client) and guide the user through installation or setup.
-
Git Utilities:
git:amend
: Automatically stage all changes and then rungit commit --amend --no-edit
.git:squash
: Interactive rebase for squashing commits (could wrap a shell script).git:clean-branches
: Remove merged local branches.
-
Performance Monitoring:
perf:benchmark
: Run performance benchmarks for specific parts of your code.perf:profile
: Start your application with a profiler attached.
-
AI/ML Integration (for relevant projects):
ml:train
: Run a script to train a machine learning model.ml:predict
: Run a script to make predictions using a trained model.
-
Cross-Platform Compatibility:
- Use
cross-env
for environment variables and other tools to ensure scripts work similarly on Windows, macOS, and Linux.
- Use
- Keep them concise: If a script becomes too long or complex, consider moving it into a separate
.js
or.sh
file in ascripts/
directory and then calling that file frompackage.json
. - Use
npm-run-all
orconcurrently
: For running multiple scripts in sequence (npm-run-all
) or in parallel (concurrently
). - Use
pre
andpost
hooks: npm automatically runspre<script>
before<script>
andpost<script>
after<script>
. This is great for chaining actions. - Descriptive names: Make script names clear and indicative of their purpose (e.g.,
test:unit
,build:prod
). - Utilize arguments: Pass arguments to your scripts using
--
(e.g.,npm run script -- --some-arg value
). - Error handling: Ensure scripts exit with appropriate status codes if they fail, so CI/CD pipelines can detect issues.
- Consistency: Establish conventions for your team on how scripts are named and used.
By leveraging package.json
scripts effectively, you can significantly streamline your development process, reduce manual errors, and create a more consistent and enjoyable developer experience.