Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Unable to require.resolve a package location when the package is 'type:module' and 'exports' is defined without exposing package.json. #547

Closed
trusktr opened this issue Aug 16, 2020 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@trusktr
Copy link

trusktr commented Aug 16, 2020

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 expose package.json).

The problem is that if a package has the following in package.json,

{
	"name": "@scope/package",
	...
	"types": "dist/index.d.ts",
	"exports COMMENT:": "This removes 'dist' from import statements, as well as replaces the 'main' field. See https://github.com/nodejs/node/issues/14970#issuecomment-571887546",
	"exports": {
		".": "./dist/index.js",
		"./": "./dist/"
	},
	...
}

Then one can not use require() or require.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 with

Uncaught Error: Cannot find module '@scope/package/package.json'

In the repl you'll see a MODULE_NOT_FOUND message.

The package author would need to expose package.json for resolving to work:

{
	"name": "@scope/package",
	...
	"types": "dist/index.d.ts",
	"exports": {
		"./package.json": "./package.json", // <---- here
		".": "./dist/index.js",
		"./": "./dist/"
	},
	...
}

I thought that exports was supposed to affect only import statements, but not require.

@trusktr
Copy link
Author

trusktr commented Aug 16, 2020

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.

@guybedford
Copy link
Contributor

This looks like a duplicate of nodejs/node#49445.

@GeoffreyBooth
Copy link
Member

Closing as a duplicate of nodejs/node#49445, @trusktr please see that issue and its referenced @nodejs/node issue.

@GeoffreyBooth GeoffreyBooth added the duplicate This issue or pull request already exists label Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants