Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
smcmurray opened this issue Mar 17, 2015 · 7 comments
Closed

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

smcmurray opened this issue Mar 17, 2015 · 7 comments
Labels
discuss Issues opened for discussions and feedbacks. module Issues and PRs related to the module subsystem.

Comments

@smcmurray
Copy link

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

@silverwind silverwind added module Issues and PRs related to the module subsystem. discuss Issues opened for discussions and feedbacks. labels Mar 17, 2015
@silverwind
Copy link
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)

@rvagg
Copy link
Member

rvagg commented Mar 18, 2015

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?

@targos
Copy link
Member

targos commented Mar 18, 2015

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

silverwind pushed a commit that referenced this issue Mar 20, 2015
Previously, the minimal argument to require the current directory was
require('./'). This commits allows to skip the trailing slash.

Fixes: #1178
PR-URL: #1185
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Christian Tellnes <christian@tellnes.no>
Reviewed-By: Roman Reiss <me@silverwind.io>
@silverwind
Copy link
Contributor

Fixed by 6fc5e95

@rlidwka
Copy link
Contributor

rlidwka commented Apr 7, 2015

@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'));

@silverwind
Copy link
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).

@rlidwka
Copy link
Contributor

rlidwka commented Apr 7, 2015

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
Labels
discuss Issues opened for discussions and feedbacks. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants