While working on a project, at some point I got this error:
在处理项目时,有时会出现此错误:
TypeError: Cannot assign to read only property 'exports' of object '#<Object>' error
The error is generated by Webpack and it means you are trying to use CommonJS while you need to use ES modules!
该错误是由Webpack生成的,这意味着您在需要使用ES模块的同时尝试使用CommonJS !
Instead of using the CommonJS syntax:
代替使用CommonJS语法:
const myfunction = () => {}
module.exports = myfunction
use this ES Modules syntax:
使用以下ES模块语法:
const myfunction = () => {}
export default myfunction
Then you can import an exported function or object like this:
然后,您可以导入导出的函数或对象,如下所示:
import myfunction from './myfunction'
You can also export multiple functions or objects from a file:
您还可以从文件中导出多个功能或对象:
myfunctions.js
myfunctions.js
const myfunction1 = () => {}
const myfunction1 = () => {}
export {
myfunction1,
myfunction2
}
Then you can import them as:
然后,您可以将它们导入为:
import { myfunction1, myfunction2 } from './myfunctions.js'
翻译自: https://flaviocopes.com/cannot-assign-readonly-property-export/