You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:44:31