Skip to content

doesn't support $HOME/.node_modules or $HOME/.node_libraries #163

Closed

Description

@mk-pmb

Hi, thanks for this module!
It seems to have trouble finding my packages, though:

$ readlink -m ~/.node_modules
/mnt/…/nodejs/modules
$ nodejs -e 'console.log(require.resolve("lodash")); require("resolve")("lodash", console.log);'
/mnt/…/nodejs/modules/lodash/lodash.js
{ Error: Cannot find module 'lodash' from '.'
    at /mnt/…/nodejs/modules/resolve/lib/async.js:50:31
    at processDirs (/mnt/…/nodejs/modules/resolve/lib/async.js:184:39)
    at ondir (/mnt/…/nodejs/modules/resolve/lib/async.js:199:13)
    at load (/mnt/…/nodejs/modules/resolve/lib/async.js:82:43)
    at onex (/mnt/…/nodejs/modules/resolve/lib/async.js:107:17)
    at /mnt/…/nodejs/modules/resolve/lib/async.js:12:69
    at FSReqWrap.oncomplete (fs.js:152:21) code: 'MODULE_NOT_FOUND' }

So require.resolve was able to find it, but resolve wasn't. However, it works when I create a symlink /mnt/…/nodejs/node_modules to modules.

Update: Work-around: It also works when I pass option { paths: [process.env.HOME + "/.node_modules"] }
Update 2: However, in that case I have to then explicitly resolve the returned path in order to get the exact same result.

Activity

  1. ljharb commented on Jun 2, 2018

    @ljharb
    Member

    I think you'll need to set the preserveSymlinks option to true.

  2. mk-pmb commented on Jun 3, 2018

    @mk-pmb
    Author

    Nope, doesn't seem to help:

    $ nodejs -e 'console.log(require.resolve("lodash")); require("resolve")("lodash", { preserveSymlinks: true }, console.log);' 
    /mnt/…/nodejs/modules/lodash/lodash.js
    { Error: Cannot find module 'lodash' from '.'
    […]
  3. mk-pmb commented on Jun 3, 2018

    @mk-pmb
    Author

    To clarify, with "explicitly resolve the returned path" I meant I have to fs.realpath() it myself.

  4. ljharb commented on Jun 17, 2018

    @ljharb
    Member

    (Possibly related to #122)

  5. safizn commented on Aug 26, 2018

    @safizn

    preserveSymlinks doesn't seem to work on Windows Symbolic link or Junction. Is there a problem with the implementation in the module ?
    It never resolves to the real path, which what I needed from this module anyway. But the options preserveSymlinks: false, should work.
    There seems to be a bug.

  6. ljharb commented on Aug 26, 2018

    @ljharb
    Member

    @myuseringithub does node itself work with windows symlinks?

  7. mk-pmb commented on Aug 26, 2018

    @mk-pmb
    Author

    I reproduced the problem and took an strace capture. The diff shows what really happened vs. what I'd expect to happen:

    -stat64("/mnt/…/nodejs/modules/node_modules/lodash/package.json", …
    +stat64("/mnt/…/nodejs/modules/lodash/package.json", …
     stat64("/mnt/…/nodejs/node_modules/lodash/package.json", …
     stat64("/mnt/…/node_modules/lodash/package.json", …
     stat64("/mnt/node_modules/lodash/package.json", …
     stat64("/node_modules/lodash/package.json", …

    It seems resolve searches for modules in module directories inside of module search path directories, but not inside the module search path directories themselves.

  8. safizn commented on Aug 27, 2018

    @safizn

    @ljharb Yes, I use symbolic link of type Junction, as it allows me to create symlinks that are supported between the host machine (Windows OS), and the Docker container (Linux OS) which I'm running the app on. In my setup both the host machine and container use JS scripts to run the project.
    Node's require.resolve('<a symlink>') returns the real path of the symlink as Node's command option --reserve-symlink is set to false by default.

  9. ljharb commented on Jan 3, 2022

    @ljharb
    Member

    I'm unclear on what needs to be done here. Could anyone provide a repro repo, or better, a failing test case?

  10. ljharb commented on Jan 11, 2022

    @ljharb
    Member

    @mk-pmb thanks - is there any way to repro it on the command line? When i run npm test I get:

    > [email protected] test
    > bash demo.sh
    
    readlink: illegal option -- m
    usage: readlink [-n] [file ...]
    demo.sh: line 11: cd: /prepare: No such file or directory
    
  11. ljharb commented on Jan 11, 2022

    @ljharb
    Member

    @mk-pmb ah! resolve 1 has preserveSymlinks: true set by default. resolve 2 has preserveSymlinks: false set by default.

    Try setting preserveSymlinks: false explicitly (or try a resolve 2 prerelease).

  12. mk-pmb commented on Jan 11, 2022

    @mk-pmb
    Author

    is there any way to repro it on the command line?

    Yes, if you run demo.sh in an environment similar enough to the GitHub Actions env.

    readlink: illegal option -- m

    You'll need GNU coreutils 8.21 or later installed. Ubuntu ships it by default, so I use that in the GitHub Actions.

    Try setting preserveSymlinks: false explicitly (or try a resolve 2 prerelease).

    Done in the no-preserve-symlinks branch, results are in.

  13. ljharb commented on Jan 11, 2022

    @ljharb
    Member
    pify?
    41    node.js resolver: /…/browserify-resolve-symlink-163/mnt/projects/webdev/js/modules/pify/index.js
    42    "resolve" module: /…/browserify-resolve-symlink-163/mnt/projects/webdev/js/modules/pify/index.js
    

    Seems like it's good! Does that mean this can be closed?

  14. mk-pmb commented on Jan 11, 2022

    @mk-pmb
    Author

    The first two tests are the part that had been easy to fix ever since. The bug in my opening post is reproduced in the 3rd test case, and it still fails:

    global-module-163?
        node.js resolver: /…/browserify-resolve-symlink-163/mnt/projects/webdev/js/secret_modules/global-module-163/dummy.js
        "resolve" module: Error: Cannot find module 'global-module-163' from '/home/runner/work/browserify-resolve-symlink-163/browserify-resolve-symlink-163/home/demo/my-cool-app'
    
  15. 8 remaining items

  16. ljharb commented on Jan 15, 2022

    @ljharb
    Member

    whoa, that was not a thing I was aware of.

    resolve has no knowledge of those paths and isn't set up to use them, and I'm not sure why anybody else would. $HOME shouldn't have any node_modules.

  17. ljharb commented on Jan 15, 2022

    @ljharb
    Member

    In other words, this issue has nothing to do with symlinks - it's about some kind of special $HOME/.node_modules path that nobody's ever mentioned in a decade of this package not supporting it :-) I'll update the title to reflect that.

    (note that "global" means npm root -g; this home dir thing is something else)

  18. changed the title [-]Doesn't find modules behind symlinks?[/-] [+]doesn't support `$HOME/.node_modules`[/+] on Jan 15, 2022
  19. ljharb commented on Jan 15, 2022

    @ljharb
    Member

    For history's sake: nodejs/node@5a49f96#diff-cdd77182e73349d6c83fbbce1cccf6f15972fbece4dbd9c52eececf1ca3bfcd5R307-R308 is in every node version since basically ever.

  20. changed the title [-]doesn't support `$HOME/.node_modules`[/-] [+]doesn't support `$HOME/.node_modules` or `$HOME/.node_libraries`[/+] on Jan 15, 2022
  21. mk-pmb commented on Jan 16, 2022

    @mk-pmb
    Author

    I updated my test project;

    • Fix the naming of the module to be resolved via ~/.node_modules
    • Add test for ~/.node_libraries
    • Add test for an actual global module

    Update: Today I learned that my global modules being require()-able system-wide is just accidential. The node design concept seems to understand npm --global as related only to modules' CLI commands, not the modules themselves.

  22. ljharb commented on Jan 16, 2022

    @ljharb
    Member

    indeed; global things aren’t supposed to be requireable.

  23. ljharb commented on Jan 18, 2022

    @ljharb
    Member

    The current workaround for this is to pass [$HOME/.node_libraries, $HOME/.node_modules] as the paths option.

  24. added a commit that references this issue on Jan 21, 2022
    2189274
  25. ljharb commented on Jan 21, 2022

    @ljharb
    Member

    @mk-pmb can you confirm that #273 works for you? (or the equivalent change on 1.x)

  26. added 3 commits that reference this issue on Jan 21, 2022
    f131ba9
    0d5a7bb
    66066fd
  27. added a commit that references this issue on Jan 22, 2022
    9bd06c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions