Sitelet https://github.com/nodejs/node/issues/1803
Skip to content

-r/--require flag for npm modules #1803

Description

@wavded

Given the following steps:

npm install heapdump
node -r heapdump file.js

I get the following error:

Error: Cannot find module 'heapdump'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at node.js:858:16
    at Array.forEach (native)
    at Function.startup.preloadModules (node.js:857:32)
    at startup (node.js:95:17)
    at node.js:963:3

My expectation was the module would be resolved the same way as if I were to do:

require('heapdump')

Activity

  1. ofrobots commented on May 26, 2015

    @ofrobots
    Contributor

    I'm investigating.

  2. ofrobots commented on May 26, 2015

    @ofrobots
    Contributor

    The problem is related to the fact that preload modules do not have a parent. Not even the main module. As such they don't use the .paths property from the parent which gives us a starting point for searching node_module directories.

  3. ofrobots commented on May 27, 2015

    @ofrobots
    Contributor

    Here's a potential fix I am evaluating:

    Module.preloadModules = function(requests) {
      if (requests) {
        // Preloaded modules have a dummy parent module which is deemed to exist
        // in the current working directory. This seeds the search path for
        // preloaded modules.
        var parent = new Module('internal/preload', null);
        parent.paths = Module._nodeModulePaths(process.cwd());
        requests.forEach(function(request) {
          Module._load(request, parent, false);
        });
      }
    }

    Note the slight semantic difference between -r foo and require(foo). The former search from the working directory of the process upwards whereas the latter starts from the directory of the requiring module.

  4. added
    moduleIssues and PRs related to the module subsystem.
    on May 27, 2015
  5. i5ting commented on May 27, 2015

    @i5ting

    the same issue

  6. i5ting commented on May 27, 2015

    @i5ting

    i want in cli i can use require 'xxx'

  7. Fishrock123 commented on May 27, 2015

    @Fishrock123
    Contributor

    Note the slight semantic difference between -r foo and require(foo). The former search from the working directory of the process upwards whereas the latter starts from the directory of the requiring module.

    Right, but that seems to make sense, the requiring "module" in this case is effectively the working directory.

  8. ofrobots commented on May 27, 2015

    @ofrobots
    Contributor

    @Fishrock123 That's my intuition too. I wanted to point it out to make sure it is not surprising to others.

  9. Fishrock123 commented on May 30, 2015

    @Fishrock123
    Contributor

    Fixed in 5759722!

  10. bmeck commented on Aug 12, 2015

    @bmeck
    Member

    Using process.cwd() makes using preloaded modules with absolute paths blow up when you are in a ENOENT directory. investigating fix

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

    confirmed-bugIssues and PRs for confirmed bugs.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions