1

What is best pratice for referencing a local commonjs module without using a relative path as below?

var custMod= require(‘customModule’);
custMod(true);
custMod.save(‘foo’);

Is there any reference for building a module like this?

If I wrote module like below, getting undefined when I call custMode.save(12);

module.exports = customModule;function customModule(mode) {
  var mode = 1;
  return {
    save: function (value) {
        console.log(mode, value)
    },
    get: function (id) {
        console.log(mode, id)
    }
}
6
  • Are you asking how to write a module that injects global variables? Or how to create a function that also has custom properties? What exactly is the "this" you are talking about? Apr 29, 2015 at 15:23
  • writing modular js Apr 29, 2015 at 15:24
  • @FelixKling I was asking about writing module that injects global variables and can be loaded using require function.
    – peterkr
    Apr 29, 2015 at 15:30
  • Still not 100% sure what you're asking for, but I've added an answer based on my assumptions. What do you mean by "injects global variables"? Apr 29, 2015 at 15:32
  • 1
    Every module can be loaded with require. Injecting global variables should be avoided. How the module can be loaded (i.e. via a direct path or just the name) cannot be influenced by the module itself. It depends on how the Node environment is set up. This may help to understand the issue: nodejs.org/api/modules.html Apr 29, 2015 at 15:32

2 Answers 2

1

You can add a path for require to check using

require.paths.push('/my/path');

or

require.main.paths.push('/my/path');

Depending on your node version.

Then if customModule.js exists at /my/path/customModule.js, you can just use

require('customModule');

Do note though, you'd need to do this on every module that you intend to use this method on.

3
  • "Otherwise, you're going to need to npm link or add your module to NPM." Not really. All that really is required is that the module is located in the package's node_modules folder. npm just makes this more convenient. But yeah, adding to npm became a bit more practical, now that it offers private modules. Apr 29, 2015 at 15:35
  • @FelixKling You're right, removed. I don't think that is what I was looking for, but I could've sworn there was an NPM-cli flag that would copy a folder into node_modules. Apr 29, 2015 at 15:37
  • Yes, I think that's what npm link would do (it links a module into the current package). I was just saying that you don't need it (I guess I'm too picky ;) ) Apr 29, 2015 at 15:38
0

I wish node made this easier to be honest. One possibility:

project_root
`--node_modules
   |--npm-module-1
   |--npm-module-2
   |--... (etc)
   `--lib
      |--my-module.js
      `--my-other-module.js

With the above you can then type require('lib/my-module') from anywhere in your project. (Just make sure and never install an npm module named lib :) Another possibility:

project_root
|--node_modules
|  |--npm-module-1
|  |--npm-module-2
|  `--... (etc)
`--lib
   `--node_modules
      |--my-module.js
      `--my-other-module.js

With the above you can then type require('my-module'), but only for any files under project_root/lib/.

An advantage of the former approach is that require('lib/my-module') makes it super easy at a glance to tell which modules are local to the project. However the latter is less typing.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.