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

package.json "main" may specify a directory; spec says it may not #9642

Closed
ghost opened this issue Nov 16, 2016 · 3 comments
Closed

package.json "main" may specify a directory; spec says it may not #9642

ghost opened this issue Nov 16, 2016 · 3 comments
Labels
doc Issues and PRs related to the documentations. module Issues and PRs related to the module subsystem.

Comments

@ghost
Copy link

ghost commented Nov 16, 2016

  • Version: v7.1.0
  • Platform: Windows 10 Enterprise version 1607, 64-bit
  • Subsystem: ?

To reproduce

a.js

console.log(require.resolve("./m"));
console.log(require.resolve("./m2"));

m/package.json

{ "main": "main" }

*m/main/index.js

(empty file)

m2/package.json

{ "main": "main" }

m2/main/package.json

{ "main": "main2" }

m2/main/main2.js

(empty file)

When I run this, I get:

C:\Users\anhans\work\sample\package\m\main\index.js
module.js:474
    throw err;
    ^

Error: Cannot find module './m2'

Explanation

Based on the spec, I would not expect the first require.resolve call to succeed.

The important line is c. LOAD_AS_FILE(M) at the end of LOAD_AS_DIRECTORY step 1.
It appears to not be LOAD_AS_FILE because m/main/index.js is loaded, but not LOAD_AS_DIRECTORY either because m2/main/main2.js is not loaded (i.e., package.json is not looked up recursively).

This is relevant because we want to follow the node module-resolution spec in our own module loader; but we also want it to work for a real packages like common-tags, which specifies the lib directory for its main. See microsoft/TypeScript#12198.

@mscdex mscdex added the module Issues and PRs related to the module subsystem. label Nov 16, 2016
@Fishrock123 Fishrock123 added the doc Issues and PRs related to the documentations. label Nov 16, 2016
@Fishrock123
Copy link
Contributor

I suspect the "spec" is out of date. We probably can't change the running behavior of this.

@bnoordhuis
Copy link
Member

It is, see #4595 (comment) for another example.

@bmeck
Copy link
Member

bmeck commented Mar 17, 2017

fixed 608ea94

@bmeck bmeck closed this as completed Mar 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants