Looking to steer clear of the complicated relative path problem detailed here by opting for one of the suggested solutions. I've found three similar libraries that could help:
I've experimented with all three and encountered "module not found" errors, leading me to believe I may be making a fundamental mistake. As someone relatively inexperienced with npm/node, I am solely using node in the browser and utilizing browserify to bundle my application into a single JS file.
Below is an extremely straightforward hello world example:
Structure:
lib/Bob.js
app.js
Bob.js
function Bob() {
return "I am bob";
}
module.exports = Bob;
app.js
var Bob = require('./lib/Bob.js');
console.log(Bob());
Bundling into a single JS:
browserify app.js -o bundle.js
The console in Chrome successfully displays "I am Bob".
When attempting any of the libraries, such as requirish:
REQUIRISH:
npm install requirish
Changes to app.js
'use strict';
require('requirish')._(module);
var Bob = require('lib/Bob');
console.log(Bob());
Changes to bundling
browserify -t requirish app.js > bundle.js
This results in the following error:
Error: Cannot find module '/lib/Bob' from '/Users/ngb/projects/MyApp/src/main/resources/public/js/hello'
at /Users/ngb/.nvm/v0.10.30/lib/node_modules/browserify/node_modules/resolve/lib/async.js:42:25
RFR:
'use strict';
var rfr = require('rfr');
var Bob = rfr('lib/Bob');
console.log(Bob());
Building process
browserify app.js -o bundle.js -d
The console in Chrome shows the following error message:
Uncaught Error: Cannot find module 'lib/Bob'