1

I write a fairly complicated node.js project, and the need to write the relative path in the "require" is driving me insane and is error prune.

Is there any way to avoid stuff like this

logger = require('../../modules/logger'); 

and be path-independent without turning every module into a full blown npm module?

2 Answers 2

1

I often use an "app symlink" where I create a symlink at node_modules/app that points to ../app. Then in my code I can require modules without relative paths: require('app/logger'). It avoids lots of ../../.. paths and having to change them when you move a file around. However, since it relies on symlinks and Windows does not support symlinks, it won't work on Windows. Thus I only use it in apps I know only need to work on posix systems.

Sign up to request clarification or add additional context in comments.

Comments

0

Depending on how your directory structure is laid out for your project, it might be best to pass things like your logger to your other modules from the main app (and farther from there).

var logger = require('./helpers/logger'); var someModule = require ('./controllers/someModule')({ logger: logger }); 

Passing other things that way help too, like the db connection (or connection pool). Again, depends on how you have your project laid out.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.