Home >Web Front-end >JS Tutorial >Supercharge `npm run dev` with package.json scripts
npm run dev is the standard for "run my website locally," but how does it work? How can we expand its functionality? In this post we'll look at:
As a motivating example, here are some scripts defined in the convex-helpers example app. We'll cover what each piece does
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "build": "tsc && vite build", "dev:backend": "convex dev", "dev:frontend": "vite", "predev": "convex dev --until-success", "test": "vitest" },
npm run runs commands that are defined in your package.json in your project's workspace. These commands are often pre-configured when you start your repo from a command like npm create vite@latest with commands for:
Here's a basic example from Next.js:
// in package.json { // ... "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, //...
Here you can run npm run dev or npm run lint etc.
You can learn more about npm run in the docs.
It's a fair question why one would put commands that already so simple into package scripts. Why not just call jest or vite or next build? There's a few good reasons:
Yes! Even if you install your dependencies with another package manager, you can still run your package scripts with npm.
yarn # similar to `npm install` npm run dev # still works!
You don't have to remember that npm run dev maps to yarn dev (or yarn run dev). The same goes for npx: npx convex dev works regardless of what package manager you used to install things.
There are a couple packages you can use to run commands concurrently:4
We'll just look at npm-run-all here. Consider our example:
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "dev:backend": "convex dev", "dev:frontend": "vite", },
This defines three scripts.
Both outputs are streamed out, and doing Ctrl-C will interrupt both scripts.
You can specify commands to run before (pre) or after (post) another command (say, X) by naming your command preX or postX. In the example:
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "dev:backend": "convex dev", "dev:frontend": "vite", "predev": "convex dev --until-success", },
This will run convex dev --until-success, before the "dev" command of npm-run-all --parallel dev:backend dev:frontend.
For those used to shell scripting, you can run two commands in sequence if the previous one succeeds with commandA && commandB. This works on both Windows and Unix (mac / linux).
However, there's a couple advantages to just using pre-scripts:
For Convex, when you first run npx convex dev (or npm run dev with the above scripts), it will ask you to log in if you aren't already, and ask you to set up your project if one isn't already set up. This is great, but interactive commands that update the output text don't work well when the output is being streamed by multiple commands at once. This is the motivation for running npx convex dev --until-success before npx convex dev.
If you change your "predev" command for Convex to include --run it will run a server-side function before your frontend has started.
"scripts": { //... "predev": "convex dev --until-success --run init", //... },
The --run init command will run a function that is the default export in convex/init.ts. You could also have run --run myFolder/myModule:myFunction. See docs on naming here. See this post on seeding data but the gist is that you can define an internalMutation that checks if the database is empty, and if so inserts a collection of records for testing / setup purposes.
If you use TypeScript, you can run a type check / compile your typescript files with a bare tsc. If your tsconfig.json is configured to emit types, it will write out the types. If not, it will just validate the types. This is great to do as part of the build, so you don't build anything that has type errors. This is why the above example did:
"build": "tsc && vite build",
If you want to pass arguments to a command, for instance passing arguments to your testing command to specify what test to run, you can pass them after a -- to separate the command from the argument. Technically you don't need -- if your arguments are positional instead of --prefixed, but it doesn't hurt to always do it in case you forget which to do it for.
npm run test -- --grep="pattern"
We looked at some ways of using package.json scripts to simplify our workflows. Who knew how much power could rest behind a simple npm run dev? Looking at our original example:
"scripts": { "dev": "npm-run-all --parallel dev:backend dev:frontend", "build": "tsc && vite build", "dev:backend": "convex dev", "dev:frontend": "vite", "predev": "convex dev --until-success", "test": "vitest" },
The way your shell finds which command to run when you type npm is to check the shell's PATH environment variable (on unix machines anyways). You can see your own with echo "$PATH". It checks all the places specified in $PATH and uses the first one. ↩
Technically you can override & specify where npm installs binaries. ↩
If you really want to, you can run npm exec vitest, npx vitest for short, ./npm_modules/.bin/vitest directly, or add .npm_modules/.bin to your PATH. ↩
Some people use a bare & to run one task in the background, but that is not supported on Windows, and interrupting one command won't necessarily kill the other. ↩
The above is the detailed content of Supercharge `npm run dev` with package.json scripts. For more information, please follow other related articles on the PHP Chinese website!