简体   繁体   English

CertStream Javascript 给出错误“Uncaught ReferenceError: require is not defined”

[英]CertStream Javascript gives error "Uncaught ReferenceError: require is not defined"

I'm setting up a HTML page that want to use the data from CertStream .我正在设置一个想要使用来自CertStream的数据的 HTML 页面。

The Javascript library is located at https://github.com/CaliDog/certstream-js Javascript 库位于https://github.com/CaliDog/certstream-js

In the install instructions it says " if you're using this in the browser, just add dist/certstream.min.js to a tag, and interact with it as normal!".在安装说明中,它说“如果您在浏览器中使用它,只需将 dist/certstream.min.js 添加到标签中,然后像往常一样与之交互!”。

I have therefor created a HTML page that uses this tag:为此,我创建了一个使用此标签的 HTML 页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Certstream</title>
    <meta name="viewport" content="width=device-width; initial-scale=1.0;"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UFT-8" />
</head>
<body>

<h1>CertStream</h1>

<!-- CertStream script -->
    <script src="dist/certstream.min.js"></script>
    <script>
    const CertStreamClient = require('certstream');

    let client = new CertStreamClient(function(message){
        console.log("Received -> ", message)
    });

    client.connect();

    </script>
<!-- //CertStream script -->


</body>
</html>

But I get the error:但我收到错误:

Uncaught ReferenceError: require is not defined http://localhost/certstream-js/test.html:15未捕获的 ReferenceError: require 未定义 http://localhost/certstream-js/test.html:15

certstream.min.js is located on in the folder "dist": certstream.min.js 位于文件夹“dist”中:

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.CertStream=t():e.CertStream=t()}(this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var c=n[o]={exports:{},id:o,loaded:!1};return e[o].call(c.exports,c,c.exports,t),c.loaded=!0,c.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function o(e){return e&&e.__esModule?e:{default:e}}function c(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),i=n(1),s=o(i),a=function(){function e(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];c(this,e),this.context={},this.callback=t,this.skipHeartbeats=n}return r(e,[{key:"connect",value:function(){var e=this;console.log("Connecting..."),this.ws=new s.default("wss://certstream.calidog.io/"),console.log("Created ws -> ",this.ws),this.ws.onmessage=function(t){console.log("onmessage called!");var n=JSON.parse(t.data);"heartbeat"===n.message_type&&e.skipHeartbeats||e.callback(t,e.context)},this.ws.onopen=function(){console.log("Connection established to certstream! Waiting for messages...")},this.ws.open()}}]),e}();t.default=a},function(e,t,n){var o,c,r;!function(n,i){c=[],o=i,r="function"==typeof o?o.apply(t,c):o,!(void 0!==r&&(e.exports=r))}(this,function(){function e(t,n,o){function c(e,t){var n=document.createEvent("CustomEvent");return n.initCustomEvent(e,!1,!1,t),n}var r={debug:!1,automaticOpen:!0,reconnectInterval:1e3,maxReconnectInterval:3e4,reconnectDecay:1.5,timeoutInterval:2e3,maxReconnectAttempts:null};o||(o={});for(var i in r)"undefined"!=typeof o[i]?this[i]=o[i]:this[i]=r[i];this.url=t,this.reconnectAttempts=0,this.readyState=WebSocket.CONNECTING,this.protocol=null;var s,a=this,u=!1,l=!1,d=document.createElement("div");d.addEventListener("open",function(e){a.onopen(e)}),d.addEventListener("close",function(e){a.onclose(e)}),d.addEventListener("connecting",function(e){a.onconnecting(e)}),d.addEventListener("message",function(e){a.onmessage(e)}),d.addEventListener("error",function(e){a.onerror(e)}),this.addEventListener=d.addEventListener.bind(d),this.removeEventListener=d.removeEventListener.bind(d),this.dispatchEvent=d.dispatchEvent.bind(d),this.open=function(t){if(s=new WebSocket(a.url,n||[]),t){if(this.maxReconnectAttempts&&this.reconnectAttempts>this.maxReconnectAttempts)return}else d.dispatchEvent(c("connecting")),this.reconnectAttempts=0;(a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","attempt-connect",a.url);var o=s,r=setTimeout(function(){(a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","connection-timeout",a.url),l=!0,o.close(),l=!1},a.timeoutInterval);s.onopen=function(n){clearTimeout(r),(a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","onopen",a.url),a.protocol=s.protocol,a.readyState=WebSocket.OPEN,a.reconnectAttempts=0;var o=c("open");o.isReconnect=t,t=!1,d.dispatchEvent(o)},s.onclose=function(n){if(clearTimeout(r),s=null,u)a.readyState=WebSocket.CLOSED,d.dispatchEvent(c("close"));else{a.readyState=WebSocket.CONNECTING;var o=c("connecting");o.code=n.code,o.reason=n.reason,o.wasClean=n.wasClean,d.dispatchEvent(o),t||l||((a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","onclose",a.url),d.dispatchEvent(c("close")));var r=a.reconnectInterval*Math.pow(a.reconnectDecay,a.reconnectAttempts);setTimeout(function(){a.reconnectAttempts++,a.open(!0)},r>a.maxReconnectInterval?a.maxReconnectInterval:r)}},s.onmessage=function(t){(a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","onmessage",a.url,t.data);var n=c("message");n.data=t.data,d.dispatchEvent(n)},s.onerror=function(t){(a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","onerror",a.url,t),d.dispatchEvent(c("error"))}},1==this.automaticOpen&&this.open(!1),this.send=function(t){if(s)return(a.debug||e.debugAll)&&console.debug("ReconnectingWebSocket","send",a.url,t),s.send(t);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},this.close=function(e,t){"undefined"==typeof e&&(e=1e3),u=!0,s&&s.close(e,t)},this.refresh=function(){s&&s.close()}}if("WebSocket"in window)return e.prototype.onopen=function(e){},e.prototype.onclose=function(e){},e.prototype.onconnecting=function(e){},e.prototype.onmessage=function(e){},e.prototype.onerror=function(e){},e.debugAll=!1,e.CONNECTING=WebSocket.CONNECTING,e.OPEN=WebSocket.OPEN,e.CLOSING=WebSocket.CLOSING,e.CLOSED=WebSocket.CLOSED,e})}])});
//# sourceMappingURL=certstream.min.js.map

What they mean by他们是什么意思

...if you're using this in the browser, just add dist/certstream.min.js to a tag, and interact with it as normal! ...如果你在浏览器中使用它,只需将 dist/certstream.min.js 添加到标签中,然后像往常一样与它交互!

...is that you don't need the require call ( require is CommonJS, not standard JavaScript, and not provided by default on browsers). ......是,你不需要require调用( require是CommonJS的,不是标准的JavaScript,并默认在浏览器上未提供)。 If you just include the script file in your page, it defines a global CertStream object with a default property providing the default export of the module.如果您只在页面中包含脚本文件,它会定义一个全局CertStream对象,该对象具有提供模块默认导出的default属性。 (I suspected this was the case, so I grabbed a copy and tried it.) (我怀疑是这种情况,所以我拿了一份副本试了一下。)

The docs could be clearer.🙂 In particular, it looks like after including the library in the browser, you have to use CertStream.default rather than CertStreamClient .文档可能会更CertStream.default特别是,在浏览器中包含库之后,您必须使用CertStream.default而不是CertStreamClient I'd probably do that by doing this up-front:我可能会通过预先执行此操作来做到这一点:

const CertStreamClient = CertStream.default;

(It's too bad they don't provide a native JavaScript module [ESM] file in their dist folder.) (太糟糕了,他们没有在dist文件夹中提供原生 JavaScript 模块 [ESM] 文件。)

I looked up into certstream.js module in the dist folder and it is a UMD module.我查看了 dist 文件夹中的 certstream.js 模块,它是一个 UMD 模块。 Basically, a UMD module is a JavaScript file that tries to guess at runtime which module system it's being used in, and then it acts as that kind of module.基本上,UMD 模块是一个 JavaScript 文件,它试图在运行时猜测它在哪个模块系统中使用,然后它充当那种模块。 So you can load the file in a plain <script> , or you can load it from an AMD module loader, or you can load it as a Node.js module, and it will always do something sensible.所以你可以在一个普通的<script>加载文件,或者你可以从 AMD 模块加载器加载它,或者你可以将它作为 Node.js 模块加载,它总是会做一些明智的事情。

在此处输入图片说明

In your code since you have already loaded the module using <script> tag, the global CertStream object can be directly used without requiring the module again.在您的代码中,由于您已经使用<script>标签加载了模块,因此可以直接使用全局CertStream对象,而无需再次使用该模块。

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

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