如何修复TypeError:无法分配为只读对象'#的属性'exports' 错误

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值