1

How should I configure multiple paths for require?

I have the following structure:

application
|-server
| |-main.js
| |-myClass.js
| |-myClass.js
| |-implementationClass.js
|-common
| |-myOtherClass.js
| |-anotherClass.js
| |-yetAnotherClass.js
|-client
| |-aClientClass.js
| |-anotherClientClass.js
| |-implementationClass.js

I want to be able to do something like this:

require('myClass');
require('myOtherClass');

How should I configure the multiple paths? currently using require.paths gives me an error : Error: require.paths is removed.

I want to keep this structure as my application has to serve static .js files from shared and I want to avoid sharing server-side .js files.

Also the files use a require() function on the client which emulates the node.js require() and I don't want to use relative paths.

the catch is that when I call require('anotherClass') it has to work on the client and on the server. So using relative paths could work but I also have the require('implementationClass') which returns either the client implementation or the server implementation, and when they are called from the common classes this approach will fail.

2 Answers 2

1

Best practice to require sub-modules is by using relative paths:

require('./server/myClass');
require('./common/myOtherClass');

If you are using requirejs, you can configure aliases for client-side:

require.config({
    baseUrl: "http://example.com/static/",
    paths: {
        "myClass": "./server/myClass",
        "myOtherClass": "./common/myOtherClass"
    }
});

I do recommend doing something like the above, but if you really want to be able to require them globally you can set or modify the NODE_PATH environmental variable before launching your app. require.paths was removed since it only caused problems.

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

3 Comments

I'm currently using NODE_PATH=`pwd`/shared node-supervisor main.js to launch the app. I hoped there was a cleaner way do do this.
No, not really. But what about adding that to a bash script?
I'm going to do that. I'll let the windows devs write their own cmd. Cheers!
0
global.mod = function (file){
    return require (path.resolve ("../..", file));
};

var myClass = mod ("server/myClass");
var myOtherClass = mod ("common/myOtherClass");

Using require with a relative path for your own modules is a very*1/0 ugly and bad approach.

3 Comments

This might work but I simply wanted to avoid adding more and more clutter to the files. I already have a client-side require and I am looking for a solution which uses require.
with my solution all your modules will be relative from your application directory, much more clean and easy to change as having the modules relative from the current script from where you call require()..., this is a caos. You have 2 options, use my approach or put your modules inside node_modules
This is not a behavior I want. Some of my classes require a module which is implemented differently on the client and differently on the server. This is why I need non relative paths. See the edited question for more details (implementationClass.js)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.