Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  96] [ 1]  / answers: 1 / hits: 37011  / 9 Years ago, tue, february 24, 2015, 12:00:00

I'm new to browserify and trying to load npm modules in browser but I'm getting the following error:




Uncaught ReferenceError: require is not defined




I'm following the tutorial from http://browserify.org/. Created javascript file with the following content:




var unique = require('uniq');




then run




npm install uniq




and




browserify main.js -o bundle.js




the bundle.js file is generated and I included it in my html but still getting the above error. Any ideas what am I doing wrong?



This is the content of final HTML file:



<!DOCTYPE html>
<html>
<head>
<title></title>

<script src=bundle.js></script>
<script src=script.js></script>
</head>
<body>

</body>
</html>


This is the content of bundle.js: http://pastebin.com/1ECkBceB



and this is script.js:




var unique = require('uniq');



More From » node.js

 Answers
29

The require function is only available in the bundle.js script context. Browserify will take all the script files necessary and put them into the bundle.js file, so you should only have to include bundle.js in the HTML file, not the script.js file.


[#67693] Saturday, February 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;