How To Import Js Library From Node_modules
Can you please explain the ideology of using import/require libraries from node_modules folder. I just want to use konva.js in my simple project, using node.js as a back end with l
Solution 1:
Node.js is a server-side runtime environment, what you need is to use the node_modules libraries on the client/browser side.
browserify will recursively analyze all the require() calls in your app in order to build a bundle you can serve up to the browser in a single script tag.
It will bundle you require and import into a single js file for use in a script tag.
Using import on client side
<scripttype="module">import {addTextToBody} from'./utils.mjs';
addTextToBody('Modules are pretty cool.');
</script>
All you need is type=module on the script element, and the browser will treat the inline or external script as an ECMAScript module
// utils.mjsexportfunctionaddTextToBody(text) {
const div = document.createElement('div');
div.textContent = text;
document.body.appendChild(div);
}
These are some great articles to get a better understanding of this :
Post a Comment for "How To Import Js Library From Node_modules"