Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix Webpack related error: Cannot assign to read only property 'exports' of object '#<Object>'?

I'm using webpack, which, if I understand correctly, doesn't support native webworker syntax, so I'm trying to use the worker-loader npm module, but I'm getting a weird error.

The module can be found here.

My webpack config:

module.exports = {
    entry: "./app",
    output: {
        path: __dirname + "/build",
        filename: "bundle.js"
    },
    watch: true,
    module: {
        rules: [{
            test: /\.worker\.js$/,
            use: {
                loader: 'worker-loader'
            }
        }]
    }
}

My code triggering the error:

import Worker from '../workers/sim.js';

class Synapse {
  // ...
}

module.exports = Synapse;

Error:

Cannot assign to read only property 'exports' of object '#<Object>'?

which points to module.exports = Synapse

like image 917
john doe Avatar asked Dec 08 '17 18:12

john doe


1 Answers

You'll get this error when mixing the CommonJS module.exports with ES Modules. You'll have to change module.exports to its ES Module counterpart export default:

import Worker from '../workers/sim.js';

class Synapse {
  // ...
}

export default Synapse; // ES Module syntax
like image 180
Christian Santos Avatar answered Oct 18 '22 05:10

Christian Santos