7

How to get __dirname to point to the preserved path of the folder rather than the real path of the symlinked file when using npm link.

Scenario

Folder Setup

project
  |
  ---- moduleA
  |       |
  |       ---- moduleA.js
  |
  ---- app
       |
       --- node_modules

cd project/moduleA
npm link
cd project/app
npm link moduleA

Inside moduleA.js

__dirname -> /project/moduleA

However, the expected behavior is

__dirname -> /project/app/node_modules/moduleA

since npm link creates a sym-link that inserts the linked module in the node_modules folder hence simulating the experience of installing the module directly.

How can I get __dirname to point to the preserved path rather than the real path where the module is located?

NOTE: --preserve-symlinks command line option does not affect the value of __dirname

takinola
  • 1,643
  • 1
  • 12
  • 23
  • `__dirname` contains the directory name of the module, which is `/project/moduleA`. The module isn't _installed_ in `/project/app/node_modules`, it's just linked there. Why do you need the path? – robertklep Aug 13 '17 at 09:55
  • The module creates and reads folders/files in other directories in the project (e.g. config and logs). I am using npm link to make development easier and avoid having to publish and update the module on every change. However, if the relative paths point to the real paths of the linked files then it would be difficult to utilize this method – takinola Aug 13 '17 at 18:53
  • 1
    You don't have a publish a module, you can also install it from its directory (`npm install /path/to/module`) – robertklep Aug 13 '17 at 19:16
  • so I can just do npm install /project/moduleA and then all changes to moduleA are instantly reflected in the project even without npm link? Cool but still doesn't solve the relative paths issue, though – takinola Aug 13 '17 at 19:18
  • 3
    No, but you can't have it both ways: it's _either_ `npm link` and deal with `__dirname` not being correct, _or_ run `npm install /path/to/module` whenever the module is changed. Or provide some sort of configuration option for your module where the caller can set/override paths (which might be a good thing anyway, modules probably shouldn't create new dirs/files in the project directory to begin with). Or assume that the current working directory is where config files are located and directories should be created. – robertklep Aug 13 '17 at 19:26
  • I agree that it is sub-optimal for modules to create new folders/files in the project directory however, putting these folders in the module directory does not work since they are removed each time npm install/update is run. This means this data is lost whenever the module is updated. Any thoughts on an alternate system would be much appreciated – takinola Aug 13 '17 at 19:36
  • Like I suggested: let the caller pass (as an option to your module) the directory in which they want the files/folders to be created, and/or use the current working directory as default (the latter is also used by configuration modules like `dotenv` and `rc`). – robertklep Aug 13 '17 at 19:53

0 Answers0