admin管理员组

文章数量:1404923

I'm installing the service following the steps at the official docs:

This piece of code:

if (process.env.NODE_ENV === 'development') {
  const { worker } = require('../tests/mocks/browser');
  worker.start();
}

Uncaught ReferenceError: require is not defined

I've setup the project using Vite. What's the correct approach to solving this.

I'm installing the service following the steps at the official docs: https://mswjs.io/docs/getting-started/install

This piece of code:

if (process.env.NODE_ENV === 'development') {
  const { worker } = require('../tests/mocks/browser');
  worker.start();
}

Uncaught ReferenceError: require is not defined

I've setup the project using Vite. What's the correct approach to solving this.

Share Improve this question asked Feb 26, 2023 at 22:02 Daniel TkachDaniel Tkach 7462 gold badges14 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Vite does not use require, try to use import instead:

if (process.env.NODE_ENV === 'development') {
  const { worker } = await import('../tests/mocks/browser');
  worker.start();
}

Source (albeit another issue, but works here as well): https://github./vitejs/vite/issues/3409#issuement-841049875

本文标签: javascriptMSW (Mock Service Worker)Vite Uncaught ReferenceError require is not definedStack Overflow