Cannot assign to read only property 'exports' of object '#<Object>' react and socket

Viewed 3590

I faced this problem while I try to use socket in react ,I know it said I should use export default instead of module.export but this piece of code exist in socket package itself and when I change it it through error

I work with node version v14.17.0

these are the packages and react version I use

  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "socket.io-client": "^4.3.1",
    "web-vitals": "^1.0.1"
  },

This is the piece of code in socket package which cause this error

Object.assign(lookup, {
    Manager,
    Socket,
    io: lookup,
    connect: lookup,
});
if (typeof module !== "undefined") {
    module.exports = lookup;
}

enter image description here

3 Answers

This error means you are trying to use commonJS inside a ES module. Instead try doing:

export default lookup

When you are instantiating the lookup object. module.exports is the commonJS way to export while webpack uses ES syntax

I know it's a bad way to solve problems, but I deleted that if statement with export

if (typeof module !== "undefined") {
    module.exports = lookup;
}

And while everything works fine

Related