by @kodeazy
Javascript require module name not loaded error?
I was trying to add a module using require
inside my .js
file.
I am getting below error.
Uncaught Error: Module name "uniq" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
at makeError (require.js:168:17)
at Object.localRequire [as require] (require.js:1436:44)
at requirejs (require.js:1797:24)
at window.loadHtml (myscripts.js:7:5)
at HTMLDivElement.onclick ((index):16:83)
I have below two files.
- example.html
- exampleTest.js
Below is my example.html
file
<html>
<head>
<script src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
<script type="text/javascript" src="exampleTest.js"></script>
</head>
<body>
<div id="displayUnique" onclick = 'displayUniqueData()'>click me</div>
</body>
</html>
Below is exampleTest.js
file
window.displayUniqueData=function (){
var arry = [1, 3, 2, 5, 3, 4,9,9]
require("uniq")(arry)
console.log(arry)
}
pre Requisites
Node
and NPM
should be installed.
To resolve the error follow the below steps
Install browserify
.
convert exampleTest.js
file into bundle bundleExample.js
file
Replace script
tag in HTML
from exampleTest.js
to bundleExample.js
.
Install browserify
.
To install browserify
execute the below command.
npm install -g browserify
Now convert exampleTest.js
file into bundle bundleExample.js
file
To convert exampleTest.js
file into bundle file execute below command.
browserify exampleTest.js -o bundleExample.js
Now bundleExample.js
is generated.
Replace script
tag insde example.html
from exampleTest.js
to bundleExample.js
.
Now open example.html
.
Add below <script>
tag inside HTML
<script type="text/javascript" src="bundleExample.js"></script>
Remove below <script>
tag inside HTML
.
<script type="text/javascript" src="exampleTest.js"></script>
Now try running the HTML
page in browser.
Output
[
1,
2,
3,
5,
9
]