RequireJS - Environment Setup



In this chapter, we will understand how to set up the environment for RequireJS. For this, you need to download the latest version of RequireJS library. You can download either the minified version or detailed version.

After downloading, we need to include the require.js file in your libs folder and the structure of your project should be as shown below −

projectname/
|--index.html
|--libs/
   |---main.js
   |---require.js
   |---helper/
      |----util.js

We need to define an html file as index.html where RequireJS is loaded as shown below.

<html>
   <head>
      <script data-main = "libs/main" src = "libs/require.js"></script>
   </head>
   
   <body>
      <h1> RequireJS Sample Page </h1>
   </body>
</html>

Note that only require.js with a RequireJS call is included in the script tag to load the script.

RequireJS in Node

There are two ways to get the Node adapter.

  • npm − You can install the latest release of requirejs from the command prompt as shown below.

npm install requirejs
  • Download r.js − You can download the r.js file from the download page and source from r.js repository page.

Advertisements