You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
global.__base = __dirname + '/';
In your very/far/away/module.js:
var Article = require(__base + 'app/models/article');
The Wrapper
Courtesy of @a-ignatov-parc. Another simple solution which increases obviousness, simply wrap the require() function with one relative to the path of the application's entry point file.
Place this code in your app.js, again before any require() calls:
global.rootRequire = function(name) {
return require(__dirname + '/' + name);
}
You can then require modules like this:
var Article = rootRequire('app/models/article');
Install some module:
npm install app-module-path --save
In your app.js, before any require() calls:
steal-prerender could offer a middelware to link all modules from
the path
function npmls(cb) {
require('child_process').exec('npm ls --depth 0 --json', function(err, stdout, stderr) {
if (err) return cb(err)
cb(null, JSON.parse(stdout));
});
}
npmls(console.log);
npm.commands.ls(args, [silent,] callback)
console.log(Object.keys(require('./package.json').dependencies));
we will probally use rootRequire try catched
---- https://gist.github.com/branneman/8048520#comment-1412502
2. The Global
In your app.js:
global.__base = __dirname + '/';
In your very/far/away/module.js:
var Article = require(__base + 'app/models/article');
Courtesy of @a-ignatov-parc. Another simple solution which increases obviousness, simply wrap the require() function with one relative to the path of the application's entry point file.
Place this code in your app.js, again before any require() calls:
global.rootRequire = function(name) {
return require(__dirname + '/' + name);
}
You can then require modules like this:
var Article = rootRequire('app/models/article');
Install some module:
npm install app-module-path --save
In your app.js, before any require() calls:
require('app-module-path').addPath(__dirname + '/app');
or Use NODE_PATH=. or project root for require
hacky method
process.env.NODE_PATH = __dirname;
require('module').Module._initPaths();
I've been using symlinks with the following structure:
it also solves the problem of not know where the modules come from because all app modules have client/server/shared prefixes in require paths
The text was updated successfully, but these errors were encountered: