How to convert node.js code to a regular javascript browser?

I do not know how to get a javascript file to work in a web browser if it is encoded as node.js.

This code applies to github graphenejs-lib . I want to convert this node.js code to js:

import {Apis} from "graphenejs-ws"; var {ChainStore} = require("graphenejs-lib"); Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then((res) => { console.log("connected to:", res[0].network); ChainStore.init().then(() => { ChainStore.subscribe(updateState); }); }); let dynamicGlobal = null; function updateState(object) { dynamicGlobal = ChainStore.getObject("2.1.0"); console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal); } 

There is another github from the same developer, steemjs-lib , which shows how to use node.js and how to use the browser on the default README page.

I do not know how to make graphenejs-lib in browser javascript, for example, steemjs-lib was turned into a normal working version of javascript. I am contacting the developer, but have not yet received a response.

I figured that other people really know how to do what dev did for steemjs-lib and make graphenejs-lib work in the browser.

Could you help me? Thanks.

+5
source share
2 answers

You can use Browserify to help you with this:

Please note that not all Node APIs will be available in the browser.

+2
source

Use a browser. https://wzrd.in/ will pack it for you if you just want to use this library. https://wzrd.in/standalone/ graphenejs-lib@latest

 <script src="https://wzrd.in/standalone/ graphenejs-lib@latest "></script> <script> const {ChainStore} = graphenejsLib; ... </script> 
+2
source

Source: https://habr.com/ru/post/1262989/


All Articles