2

I am quite new to node_moduels.

For instance, say you have this structure:

The app directory

  • your_app/node_modules/your_module
  • your_app/app.js

    The module directory

  • your_module/index.js

  • lib/a_file_you_require_in_index.js

When you require the file inside the index.js. How do you make sure the path is correct, when the code is executed inside the app.js file, where the module is required?

your_module is a npm module

ole-jorgen
  • 21
  • 3
  • 1
    file paths in node.js are relative to the filepath of the file which is calling require. so use relative path semantics to require your file. – r3wt Mar 18 '19 at 19:33
  • Yeah. However, I wish there was a way of saying "I am targeting files inside this module". For instance, if you could write: require(thisModule + 'lib/a_file_to_require'). I guess you could maybe use the path module to dynamically get the location of the module. – ole-jorgen Mar 18 '19 at 19:49
  • if you are talking about a module installed with npm, then you can do like `require('some-module/lib/some-file');`. if node doesn't know path to module it will be tricky, but the example you give is totally possible. you can use variables in require. you can require sub modules of node module. as for now its really unclear exactly what you are asking. – r3wt Mar 18 '19 at 20:02
  • Sorry for making it unclear. Forgot entierly to mention I was thinking of a npm module. – ole-jorgen Mar 18 '19 at 20:10

0 Answers0