Skip to content

require should treat "." and ".." like any other directory names #1178

Description

@smcmurray

require(".") should load ./index.js
require("..") should load ../index.js

Activity

  1. added
    moduleIssues and PRs related to the module subsystem.
    discussIssues opened for discussion and feedback.
    on Mar 17, 2015
  2. silverwind commented on Mar 17, 2015

    @silverwind
    Contributor

    I too once expected require('.') to work, but turns out you need to do require('./'). Is there a specific reason we need that trailing slash inside therequire path?

    Here's the current error:

    > require('.')
    Error: Cannot find module '.'
        at Function.Module._resolveFilename (module.js:320:15)
        at Function.Module._load (module.js:262:25)
        at Module.require (module.js:349:17)
        at require (module.js:368:17)
        at repl:1:1
        at REPLServer.defaultEval (repl.js:116:27)
        at bound (domain.js:254:14)
        at REPLServer.runBound [as eval] (domain.js:267:12)
        at REPLServer.<anonymous> (repl.js:269:12)
        at emitOne (events.js:77:13)
    
  3. rvagg commented on Mar 18, 2015

    @rvagg
    Member

    I can't think of a good reason so I'm +1 on this unless someone comes up with something, perhaps @isaacs can chime in?

  4. targos commented on Mar 18, 2015

    @targos
    Member

    @smcmurray require('..') does already load the correct file

  5. added a commit that references this issue on Mar 20, 2015
  6. silverwind commented on Mar 20, 2015

    @silverwind
    Contributor

    Fixed by 6fc5e95

  7. rlidwka commented on Apr 7, 2015

    @rlidwka
    Contributor

    @silverwind ,

    I know it's nitpicking, but... ..test is still treated incorrectly (as a local file, but it should be a module).

    Since we're bikeshedding the hell out of it anyway, my suggestion for iojs@2.0 is this:

    // old method
    /*function isRelative(p) {
      start = p.substring(0, 2);
      if (start !== '.' && start !== './' && start !== '..') {
        return false;
      } else {
        return true;
      }
    }*/
    
    // proposed method
    function isRelative(p) {
      if (p[0] !== '.') return false;
      if (p.length === 1) return true; // "."
    
      if (p[1] !== '.') return p[1] === '/'; // "./"
      if (p.length === 2) return true; // ".."
    
      return p[2] === '/';
    }
    
    /*
     * Tests
     */
    var assert = require('assert');
    
    assert(isRelative('.'));
    assert(isRelative('./foo'));
    assert(isRelative('..'));
    assert(isRelative('../foo'));
    
    assert(!isRelative('.foo'));
    assert(!isRelative('..foo'));
  8. silverwind commented on Apr 7, 2015

    @silverwind
    Contributor

    Good suggestion. I (and whoever wrote those module parts) didn't even think about module names starting with dots. I wonder if these are valid npm module names (e.g. they can be published).

  9. rlidwka commented on Apr 7, 2015

    @rlidwka
    Contributor

    They are not valid module names, npm forbids anything starting with a dot, see validate-npm-package.

    So it's purely for consistency reasons. Right now you can create file named "..foo.js" and require('..foo') it. It looks like a module, but it's not. I'd like to discourage that.

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

    discussIssues opened for discussion and feedback.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