Get __dirname to point to folder path rather than real path of sym-linked folder
Asked Answered
S

0

8

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

Seafaring answered 13/8, 2017 at 7:32 Comment(7)
__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?Cornish
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 methodSeafaring
You don't have a publish a module, you can also install it from its directory (npm install /path/to/module)Cornish
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, thoughSeafaring
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.Cornish
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 appreciatedSeafaring
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).Cornish

© 2022 - 2024 — McMap. All rights reserved.