mswjs / http-middleware

Spawn an HTTP server from your request handlers or apply them to an existing server using a middleware.
https://npm.im/@mswjs/http-middleware
107 stars 13 forks source link

Server breaks by simply adding @mswjs/data due to strict-event-emitter mismatch #27

Closed lgibso34 closed 10 months ago

lgibso34 commented 1 year ago

package.json

{
  "name": "msw-practice",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@mswjs/data": "^0.11.2",
    "@mswjs/http-middleware": "^0.7.0",
    "msw": "^1.0.1",
    "ts-node-dev": "^2.0.0",
    "typescript": "^4.9.5"
  }
}

server.ts

import { createServer } from "@mswjs/http-middleware";
import { rest } from "msw";

type Test = {
  hello: string;
};

const handlers = [
  rest.get("/user", (req, res, ctx) => {
    return res(ctx.json({ firstName: "John" }));
  }),
];

const httpServer = createServer(...handlers);

httpServer.listen(9090);
console.info("Server started");

ran with npx ts-node-dev server.ts throws an error

strict_event_emitter_1.Emitter is not a constructor at Object. (/Users/l.../node_modules/@mswjs/http-middleware/lib/middleware.js:17:17)

if you yarn remove @mswjs/data it works fine. However, if you need to keep @mswjs/data you have to add yarn add strict-event-emitter@^0.4.0 and it will work.

Hopefully you all can figure this one out. I couldn't find an older version of strict-event-emitter laying around for it to fail. Not sure what the issue is.

Thanks!

rzeinalli commented 1 year ago

Having same issue without even having @msw/data on my package list.

zephraph commented 1 year ago

After digging more into this I think the root is that different msw libraries have this dependency pinned at different versions and given it's a pre-1.0.0 release there are breaking changes shipped in minor versions. If you're using npm you can run npm why strict-event-emitter to show which packages have which dependency.

lgibso34 commented 10 months ago

I think this is solved now with https://github.com/mswjs/data/pull/278 being merged. I recently had another project that used these libraries and I did not see this issue resurface.