Access modules relative to fixed root with requirement in nodeJS

I'm trying to access modules, but nesting is getting a little out of hand

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

Can i use require("Folder/Deeper/somefile")

I tried setting

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

but it doesn't seem to work, and also feels a little ugly for me.

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

+5
source share
2 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 top of the search list

require.paths.unshift('/usr/local/node');
+4
source

(, ./app), - node_modules :

ln -s ../app ./node_modules

( app.)

require :

require('app/route/api')

package.json postinstall script npm install:

"scripts": {
  "postinstall": "ln -sf ../app ./node_modules"
}

Windows ln, Windows . Windows.

0

All Articles