Upstream Bug (?): Arcane resolution rules with package.json
Opened this issue · 0 comments
ryan-roemer commented
For a contrived example like:
// pkgs/index.js
const dep = require("./dep");
console.log("TODO HERE", { dep });
// pkgs/dep/deeper/index.js
module.exports = "deeper index";
// pkgs/dep/deeper/package.json
{
"main": "diff.js"
}
// pkgs/dep/deeper/diff.js
module.exports = "diff";
// pkgs/dep/index.js
module.exports = "dep index";
// pkgs/dep/package.json
{
"main": "deeper"
}
Node.js resolves thusly:
$ node pkgs
TODO HERE { dep: 'deeper index' }
which means pkgs/dep/package.json
is read, but pkgs/dep/deeper/package.json
is not, instead just using pkgs/dep/deeper/index.js
.
In trace-deps
, the resolve
library instead comes up with our traced deps as:
// out/pkgs/index.js
const dep = require("./dep");
console.log("TODO HERE", { dep });
// out/pkgs/dep/deeper/package.json
{
"main": "diff.js"
}
// out/pkgs/dep/deeper/diff.js
module.exports = "diff";
// out/pkgs/dep/package.json
{
"main": "deeper"
}
// out/package.json
{
/* snipped */
}
The problem is for the nested load of deeper
, resolve()
is using deeper/package.json:main
and real Node.js doesn't instead using deeper/index.js
directly, which we see in output here:
$ node out/pkgs/index.js
internal/modules/cjs/loader.js:305
throw err;
^
Error: Cannot find module '/Users/rye/Desktop/TD_TEST/out/pkgs/dep/deeper'. Please verify that the package.json has a valid "main" entry
at tryPackage (internal/modules/cjs/loader.js:297:19)
at Function.Module._findPath (internal/modules/cjs/loader.js:528:18)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:780:27)
at Function.Module._load (internal/modules/cjs/loader.js:686:27)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/Users/rye/Desktop/TD_TEST/out/pkgs/index.js:1:13)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32) {
code: 'MODULE_NOT_FOUND',
path: '/Users/rye/Desktop/TD_TEST/out/pkgs/dep/package.json',
requestPath: './dep'
}