Angular项目中使用DOMPurify 3.0.5时遭遇TS2309及模块导入错误的解决方法求助
Angular项目中使用DOMPurify 3.0.5时遭遇TS2309及模块导入错误的解决方法求助
大家好,我维护着一个基于Angular的项目,当前使用的Node版本是14.17.3。不知道最近哪里出了变动,突然就碰到了DOMPurify 3.0.5的导入问题,报错信息如下:
Error: export 'sanitize' (imported as 'DOMPurify') was not found in 'dompurify' (possible exports: default)
我尝试了以下几种解决方式:
- 在
tsconfig.json中添加配置:"allowSyntheticDefaultImports": true - 修改导入语句为:
import { default as DOMPurify } from 'dompurify'; - 在
package.json中安装了@types/dompurify: ^3.0.2类型包
但做完这些之后,又出现了新的TypeScript错误:
dompurify/dist/purify.cjs.d.ts:401:1 - error TS2309: An export assignment cannot be used in a module with other exported elements. 401 export = _default;
于是我又尝试直接指定导入ES模块文件:
import { default as DOMPurify } from 'dompurify/dist/purify.es.mjs';
结果还是不行,这次的报错是:
Module not found: Error: Package path ./dist/purify.es.mjs is not exported from package ..\node_modules\dompurify (see exports field in ..\node_modules\dompurify\package.json)
有没有朋友遇到过类似的问题?求指点解决办法,谢谢!
备注:内容来源于stack exchange,提问作者user3714598
相关产品推荐
相关产品推荐

