- Introduction
- Quick start
- Philosophy
- Comparison
- Default behaviors
- Limitations
- Debugging runbook
- FAQ
- Mocking HTTP
- Mocking GraphQL
- Mocking WebSocket
- Integrations
- API
- CLI
- Best practices
- Recipes
init
Copy the worker script into the given directory.
Usage
npx msw init <PUBLIC_DIR> [options]Arguments
PUBLIC_DIR
A relative path to the public directory of your application.
For example, when using NextJS, the public directory is located in /public, and that’s the path you should provide to this command:
npx msw init ./publicFlags
--save
Save the given PUBLIC_DIR in package.json for future automatic updates of the worker script.
npx msw init ./public --saveRunning this command will save the ./public directory in your package.json:
// package.json
{
"name": "my-app",
"msw": {
"workerDirectory": "./public"
}
}If this property is present, whenever you install the msw package, the worker script will be copied to the msw.workerDirectory destination automatically. This ensures the worker script being in sync with the currently installed version of the library.
As a counterpart to the
--saveflag, you can provide the--no-saveflag that will not prompt you to save the worker script location. This can be handy when running MSW in automated environments where terminal interactivity is not desirabble. Note that even with the--no-saveflag provided, if you have worker script locations previously saved undermsw.workerDirectoryinpackage.json, MSW will still update those locations as a part of theinitcommand.
Related materials
Managing the worker
Learn how to manage the worker script updates.