简体   繁体   English

从 NPM 包中引用对象时出现 ReferenceError

[英]ReferenceError when referring to object from NPM package

I'm trying to use https://github.com/iamcal/js-emoji to implement colon-to-emoji conversion.我正在尝试使用https://github.com/iamcal/js-emoji来实现冒号到表情符号的转换。 I installed its NPM package and put我安装了它的 NPM 包并把

<script src="../node_modules/emoji-js/lib/emoji.js" type="text/javascript;" charset="UTF-8;"></script>

in the head of my HTML to import the script.在我的 HTML 头部导入脚本。 I know this is the correct path because when I command-click it in VS Code it takes me to this file.我知道这是正确的路径,因为当我在 VS Code 中使用命令单击它时,它会将我带到该文件。 I get no type mismatch errors in the dev console from this.由此,我在开发控制台中没有收到任何类型不匹配错误。

However, when I declare a new EmojiConvertor object at the top of my script with const emoji = new EmojiConvertor();但是,当我使用const emoji = new EmojiConvertor(); , the script no longer functions and I get a ReferenceError: Uncaught ReferenceError: EmojiConvertor is not defined . ,脚本不再起作用,我得到一个 ReferenceError: Uncaught ReferenceError: EmojiConvertor is not defined

Does anyone know what I can do to fix this?有谁知道我能做些什么来解决这个问题? Thanks in advance.提前致谢。

The script declaration should not have a ; script声明不应该有; in the type="text/javascript;"type="text/javascript;" attribute.属性。

This snippet with text/javascript;这个带有text/javascript; reproduces ReferenceError :重现ReferenceError

 <html> <head> <title>https://stackoverflow.com/questions/66858758</title> </head> <body> <div id="emoji" style="text-align: center; font-size: 120px"></div> </body> <script src="https://cdn.jsdelivr.net/npm/emoji-js@3.7.0/lib/emoji.js" type="text/javascript;" charset="UTF-8"></script> <script> const emojiConvertor = new EmojiConvertor(); emojiConvertor.replace_mode = 'unified'; document.getElementById('emoji').innerHTML = emojiConvertor.replace_colons(':smile:'); </script> </html>

The same snippet removing ;删除相同的片段; and using just text/javascript solves ReferenceError and emoji-js works:并且只使用text/javascript解决ReferenceErroremoji-js工作:

 <html> <head> <title>https://stackoverflow.com/questions/66858758</title> </head> <body> <div id="emoji" style="text-align: center; font-size: 120px"></div> </body> <script src="https://cdn.jsdelivr.net/npm/emoji-js@3.7.0/lib/emoji.js" type="text/javascript" charset="UTF-8"></script> <script> const emojiConvertor = new EmojiConvertor(); emojiConvertor.replace_mode = 'unified'; document.getElementById('emoji').innerHTML = emojiConvertor.replace_colons(':smile:'); </script> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM