javascriptgitrequirewatchify

Watchify - Uncaught ReferenceError: require is not defined


i need Node.js feature like require() function in my javascript code so i am using watchify, but it is still giving me error Uncaught ReferenceError: require is not defined at jsfile.js:3 despite the fact that watchify bundles code and inspects into bundle.js file, everything would be much easier if i was testing on localhost but i have hosted my website in heroku and i do not know queue of commands, should i do git commands (git add . git commit -m "commit" git push heroku master) and then watchify (npm run watch) or first watchify and than git commands? or there is problem somewhere else? here is my package.json code (part)

"scripts": {
  "start": "node ilia.js",
  "watch": "npx watchify ./public/jsfile.js -o ./public/bundle.js"
},

"devDependencies": {
  "watchify": "^3.11.1"
}

and linking of javascript files (bundle.js, jsfile.js) in my html

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

so what should i do?


Solution

  • Watchify creates a bundle file that has all of your code with the require()s handled.

    That does not make require() work in your original code; you should only include the bundle in your HTML page and not the original JS files.