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
__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? – Cornishnpm install /path/to/module
) – Cornishnpm link
and deal with__dirname
not being correct, or runnpm 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. – Cornishdotenv
andrc
). – Cornish