Dirname Does Not Follow Symlink
Node’s global object __dirname returns the name of the directory that the currently executing script resides in. However, if a directory is symbolic linked or a symlink, it still return the original path.
For example, the following directory structure:
app.js contains the following line:
Run the script:
You get the directory the
app.js script is residing it. But if you do the same from the symlinked directory:
Well, you get the same answer, even the current working directory is different:
The return of
__dirname does not follow symbolic link.
So, have to be extra careful when requiring files that is symbolic linked. If you think you are in
lib/bar directory and try to require the
util.js script in
Both statements will throw module not found error. Path join does not help either.
The best practice is to avoid symlinks and relative directory requiring. Instead, set up required as modules, and install them in
node_modules directory, then they will become top-level modules.