NodeJS TypeError:Entities不是构造函数

e5nszbig  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(201)

一个JavaScript HTTP触发器Azure函数在Slack的在线客户社区中发布活动。
它已经成功运行了很多年,但是在从版本~2升级到~4时,开始在以下代码中抛出Entities is not a constructor错误:

var Entities = require('html-entities').AllHtmlEntities;
var entities = new Entities();
var title = entities.decode(subject)

目标是在subject变量的内容上使用html-entities
上面的代码显示在这个简短的GitHub script的上下文中。
这个错误的大多数解决方案似乎都建议使用import而不是requires,并相应地更改代码。但是,当import语句位于模块的主体中、模块的顶部以及模块的外部时,Azure函数会发出抱怨,即使模块的顶部是它的正确位置。
许多其他解决方案建议在各种配置中将代码的各种迭代放在新函数的内部和外部,但我没有成功。如果有帮助的话,我可以分享我的尝试的例子。
感谢您的耐心,因为我不是一个程序员-只是自动化的解决方案作为我的工作的一部分。我感谢任何建议,包括这是否不是实现我目标的理想方式。

fiei3ece

fiei3ece1#

参见CHANGELOG.md
关于CHANGELOG.md
htmlEntitiesInstance.encode(text) -> encode(text)
之前:

import {AllHtmlEntities} from 'html-entities';

const entities = new AllHtmlEntities();
console.log(
    entities.encode('<Hello & World>')
);

之后:

import {encode} from 'html-entities';

console.log(
    encode('<Hello & World>')
);

你的代码应该是:

var decode = require('html-entities').decode;
var title = decode(subject)

相关问题