Closed trusktr closed 4 years ago
Ah, the ESM doc says
"exports" are not specific to ES modules or CommonJS
So apparently it is supposed to work this way.
Maybe we need a new API, like require.resolvePackageLocation(moduleName)
?
In the meantime, the resolve-package-path package works great with any package regardless of what it has for exports
.
This looks like a duplicate of https://github.com/nodejs/node/issues/49445.
Closing as a duplicate of nodejs/node#49445, @trusktr please see that issue and its referenced @nodejs/node issue.
First, see this popular question on StackOverflow, whose solution is now broken for some packages using Node ESM (with
"type": "module"
and an"exports"
field that does not explicitly exposepackage.json
).The problem is that if a package has the following in
package.json
,Then one can not use
require()
orrequire.resolve()
to resolve package locations like the can with normal CommonJS packages.For example, trying to run
require.resolve('@scope/package/package.json')
will fail withIn the repl you'll see a
MODULE_NOT_FOUND
message.The package author would need to expose
package.json
for resolving to work:I thought that
exports
was supposed to affect onlyimport
statements, but notrequire
.