Search code examples
node.jsaxiosbrowserifyrequire

how to use require in main.js


I try to use axios in my main.js. i tried import axios from 'axios'. ... I got error for import. I read here that I should use require instead of import. after that I tried

 var axios=require('axios');

but I got the error ....require is not defined.

after that I read about browserify. I installed it

npm install -g browserify 

after that I used this code to bundle it

browserify main.js -o bundle.js

and I add this script to my index.html code.

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

but I got the same error

REQUIRE IS NOT DEFINED!! I am actually confused. can somebody HELP ME!

appreciated


Solution

  • By default require() is not a valid function in client side Javascript and is primarily used in server side Node.js. I recommend you look into require.js as this does extend the client side to provide you with that function. Or else go with ES6 import.