+ 5

require undefined / external modules not supported [Solved]

Hi, I am using a js library and i want to use it in my js file. For that I need to add like below statements const chalk = require(“chalk”); Or import chalk from ‘chalk’; (Es6) Both giving me errors as which is my question title. Note: I used require.js too but it didn’t work for me.

4th Mar 2021, 4:03 PM
Sharique Khan
Sharique Khan - avatar
4 Antworten
+ 3
Karak10 Thanks it worked with Browserify. We need to install broswerify. npm install browserify Then create a bundle of main js browserify ./main.js > ./bundle.js Link that bundle.js to your html. And it worked!
5th Mar 2021, 6:57 PM
Sharique Khan
Sharique Khan - avatar
+ 2
I'm pretty new to using modules so have also been facing some problems and I'm not the most qualified person to help you but I will tell you what I have understood about modules so far. If you are trying to run a JavaScript code in a browser the require() method will not exist, require method is used in Node.js from what I understand, and if you want to add modules in your code and run it on browser you should either use ES6 import method or use require() method and use Browsify to bundle your JavaScript files, I will link here a video you can watch about how Browsify works: https://youtu.be/_dtnD_8Att0 About ES6 import method, I also have problems figuring out how to make it work, the method it self works fine, my problem is that it doesn't find the modules, and I actually asked for help in SoloLearn too few hours ago but I don't have an answer yet. I don't know what kind of error it is that you get though, I think you will get a CORS policy error if you aren't running it in an online server.
4th Mar 2021, 4:21 PM
Karak10
Karak10 - avatar
+ 2
Sharique Khan if you don't want to use require() method and want to use ES6 import you can use parcel or webpack to bundle your files, webpack is a little hard to use, so I recommend parcel, here is a tutorial: https://youtu.be/OK6akGZCC88
7th Mar 2021, 4:14 PM
Karak10
Karak10 - avatar
+ 2
Thanks Karak10 but it seems someones not happy with my question and answer here. getting downvotes witout a reason 😅
7th Mar 2021, 4:18 PM
Sharique Khan
Sharique Khan - avatar