Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing modules relatively from a fixed root with require in nodeJS

I'm trying to access modules but the nesting gets a bit out of hand

require("../../../Folder/Deeper/someFile")

Is there anyway to just use require("Folder/Deeper/somefile")

I've tried setting

require.paths = ['/media/work/Project'];

but that doesn't seem to work and also feels a bit ugly to me.

Are there any alternatives. Is there any way to write a wrapper for this?

like image 666
Raynos Avatar asked Oct 13 '22 20:10

Raynos


1 Answers

Maybe this?

require.paths.unshift( '../../..' );
require("Folder/Deeper/somefile");

http://nodejs.org/api.html says:

require.paths An array of search paths for require(). This array can be modified to add custom paths.

Example: add a new path to the beginning of the search list

require.paths.unshift('/usr/local/node');
like image 179
thejh Avatar answered Oct 18 '22 09:10

thejh