Is there way to include other JavaScript source?

Note that if you’re not using webpack, you’ll need to use relative paths. The require function does not do node-style resolution or lookup.

As such:

  • require("module.js") will fail because it’s not relative. Using require("./module.js") will work, assuming it’s in the same directory.
  • If you’ve got node_modules/commonLib.js, you’ll need to use require("./node_modules/commonLib.js") (assuming main.js is i

Be sure to also use module.exports = {/*...*/} when doing this (ES6-style exports are not supported using this mechanism.)

If you want to use ES6 modules or have node-style resolution, a bundler is a requirement.

2 Likes