WP Scripts is the official command-line toolkit bundled with WordPress that automates common development tasks like building blocks, enqueuing assets, and running a local dev server. This guide shows you how to install it, scaffold a new block or plugin, and use its most useful commands—so you can skip manual setup and focus on writing code.
What You’ll Need
- A computer running macOS, Linux, or Windows 10+ (with WSL recommended for Windows).
- Node.js (version 16 or later) and npm installed. Check with
node -vandnpm -v. - A WordPress development environment (local like Local, or a remote server with SSH access).
- Basic familiarity with the command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
Step-by-Step
- Install WP Scripts globally (optional but recommended). Open your terminal and run
npm install -g @wordpress/scripts. This makes thewp-scriptscommand available everywhere. If you prefer not to install globally, you can usenpx @wordpress/scriptsinstead. - Navigate to your project folder. For a plugin, go to
wp-content/plugins/in your WordPress installation. For a theme, go towp-content/themes/your-theme/. - Create a new block or plugin (if starting fresh). Run
npx @wordpress/create-block my-block(replacemy-blockwith your desired slug). This scaffolds a complete block plugin with all necessary files. For a plain plugin, just create a folder with a PHP file manually. - Initialize npm in your project (if not already done). Run
npm init -yto create apackage.jsonfile. Thecreate-blockcommand does this automatically. - Add WP Scripts as a dev dependency. Run
npm install --save-dev @wordpress/scripts. This adds the necessary scripts to yourpackage.json. - Add useful npm scripts to your package.json. Edit the
scriptssection to include:"scripts": { "start": "wp-scripts start", "build": "wp-scripts build", "lint:js": "wp-scripts lint-js", "lint:css": "wp-scripts lint-style", "format": "wp-scripts format" }Save the file.
- Run the development server. Execute
npm startin your terminal. This compiles your JavaScript and CSS in watch mode, automatically rebuilding when you save changes. It also serves your assets from a local URL (usuallyhttp://localhost:8888). - Edit your source files. For a block, edit the
src/directory—index.js,style.scss, andeditor.scss. For a plugin, you can put your JS insrc/index.jsand CSS insrc/style.css. - Build for production. When you’re ready to deploy, run
npm run build. This creates minified, optimized files in thebuild/folder. The PHP files will enqueue these built assets automatically if you use the standard block structure. - Lint and format your code. Run
npm run lint:jsandnpm run lint:cssto check for coding standards. Usenpm run formatto automatically fix formatting issues.
Common Mistakes
- Forgetting to run
npm installafter cloning a project. Always runnpm installto install dependencies listed inpackage.json. - Using
npm startin production. The start command is for development only; it creates unminified files and watches for changes. Usenpm run buildfor production. - Not enqueuing the built assets correctly. If you’re not using the standard block structure, you need to manually enqueue the
build/index.jsandbuild/index.cssfiles usingwp_enqueue_scriptandwp_enqueue_stylein your PHP. - Editing files in the
build/folder. That folder is generated; always edit source files insrc/and rebuild. - Ignoring Node version requirements. WP Scripts requires Node 16+. Using an older version leads to cryptic errors.
Tips
- Use
wp-scripts plugin-zipto create a zip file of your plugin for distribution. Add a script:"plugin-zip": "wp-scripts plugin-zip". - Take advantage of
wp-scripts start --hotto enable hot reloading for faster development. - Use the
--webpack-src-dirflag if you want to keep your source files in a different directory thansrc/. - Combine WP Scripts with
@wordpress/envfor a complete local WordPress environment:npx @wordpress/env start. - Check the official WordPress Scripts documentation for a full list of commands and options.
Troubleshooting
If something goes wrong, here’s what to try:
- Command not found: wp-scripts – Make sure you installed it globally or as a dev dependency. Try
npx wp-scripts --versionto verify. - Node version errors – Run
node -v. If it’s below 16, update Node.js from the official website or use a version manager like nvm. - Build fails with syntax errors – Check the error message for the specific file and line. Often it’s a missing import or a typo. Run
npm run lint:jsto catch issues early. - Changes not showing in the browser – Ensure
npm startis running and you’re viewing the site via the correct URL (usuallyhttp://localhost:8888). Clear your browser cache or hard refresh (Ctrl+Shift+R). - Assets not loading in WordPress – Verify that the enqueue functions in your PHP reference the correct file paths. For blocks, the
block.jsonfile should have the correcteditorScriptandstylefields. - Port already in use – If the dev server can’t start because port 8888 is taken, use
npm start -- --port 3000to specify a different port.
Frequently Asked Questions
- Do I need WP Scripts for every WordPress project? No, it’s optional. It’s most useful for block development and modern JavaScript-heavy plugins. Simple themes can use traditional PHP and CSS without it.
- Can I use WP Scripts with a classic theme? Yes, you can use it to compile JS/CSS for any theme. Just set up your
src/folder and enqueue the built files in yourfunctions.php. - What’s the difference between
npm startandnpm run build?npm startruns in development mode with watch and source maps;npm run buildcreates minified production files. - Can I use TypeScript with WP Scripts? Yes, WP Scripts supports TypeScript out of the box. Just create
.tsor.tsxfiles in yoursrc/folder. - How do I update WP Scripts? Run
npm update @wordpress/scriptsin your project, ornpm install -g @wordpress/scripts@latestfor the global version. - Does WP Scripts work with Gutenberg blocks? Absolutely—it’s the recommended tool for block development. The
create-blockcommand scaffolds a block that uses WP Scripts by default.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Business partnership due diligence checklist what to verify before you sign?
- Does the FCC Protect Internet Privacy?
- Disaster Robbers Codes? Meaning & Origin
- Repair a Disk with Mac Disk Utility
- Avoid Vendor Lock-In: Cloud Provider Strategy
- Secure GitHub Accounts & Repos Against Malicious Code