Home > Blockchain >  how to use require in browser? js / html
how to use require in browser? js / html

Time:03-01

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?

CodePudding user response:

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 (e.g. 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.

  • Related