init
Places the Service Worker file into a given public directory of your application.
Usage
1npx msw init <PUBLIC_DIR> [options]
Arguments
PUBLIC_DIR
A relative path to the public directory of your application.
1npx msw init ./public
Options
--save
Save the given PUBLIC_DIR in your package.json for future use.
1npx msw init ./public --save
Your public directory will be saved in package.json under the msw.workerDirectory path:
1{2 "name": "your-app",3 "msw": {4 "workerDirectory": "public"5 }6}
You can also use
--no-saveto prevent the prompt and never save your public directory.
Where is my "public" directory?
A public directory is usually a root directory of your server (i.e. ./build, ./public, or ./dist). This directory is often committed to Git, and so should be the Mock Service Worker.
Common public directories
Below you can find a list of public directories in most used JavaScript project starters.
| Project name | Public directory |
|---|---|
| Create React App | ./public |
| GatsbyJS | ./static |
| NextJS | ./public |
| VueJS | ./public |
| Angular | ./src (and add it to the assets of the angular.json file) |
| Preact | ./src/static |
| Ember.js | ./public |
| Svelte.js | ./public |
| SvelteKit | ./static |
| Vite | ./public |
Not sure where is your public directory? Reach out to the maintainers of the development stack that you are using, they should be able to help.