简体   繁体   中英

how to use require in browser? js / html

I searched whole stackoverflow to fix it, but cant. So... I need to use NPM module in browser (index.html).

Getting error "require is not defined" if using

<script src="./script.js"></script>

in html file, and using const { WebcastPushConnection } = require('tiktok-livestream-chat-connector'); in script.js.

Tried to use browserify, and when i use outputed js script getting error "require.resolve is not a function"

Can anyone help?

tiktok-livestream-chat-connector is described,by its website , as:

A Node.js module to receive and decode livestream events like comments and gifts in realtime from TikTok LIVE by connecting to TikTok's internal WebCast push service.

At no point in its documentation does it mention compatibility with web browsers.

Browserify (and similar tools) can bundle up CommonJS modules into a single file so that they can run in environments which don't support CommonJS modules (eg browsers). They can't polyfill all the Node.js features that browsers don't support (like the ability to make raw socket network connections).

If a module needs to run on Node.js then you can't just move it to the browser.

You could write a program for Node.js that runs as a web service and operates as a bridge to whatever the module does. The the browser could connect to your web service with WebSockets or Ajax requests. The project links to an example of one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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