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 ./public

Flags

--save

Save the given PUBLIC_DIR in package.json for future automatic updates of the worker script.

npx msw init ./public --save

Running 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 --save flag, you can provide the --no-save flag 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-save flag provided, if you have worker script locations previously saved under msw.workerDirectory in package.json, MSW will still update those locations as a part of the init command.

Managing the worker

Learn how to manage the worker script updates.