enhanced-resolve icon indicating copy to clipboard operation
enhanced-resolve copied to clipboard

Can't resolve imports field

Open mo36924 opened this issue 3 years ago • 5 comments

Can't resolve imports field. The file structure of project:

project/
├── dist
│   ├── a.mjs
│   ├── b.mjs
│   ├── c.mjs
│   └── d.mjs
├── node_modules
└── package.json
The content of the ./package.json file:
{
  "name": "project",
  "imports": {
    "#d": {
      "import": "./dist/d.mjs",
      "node": "./dist/d.mjs"
    }
  },
  "dependencies": {
    "enhanced-resolve": "^5.3.2"
  }
}
The content of the ./dist/a.mjs file:
import resolve from "enhanced-resolve";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
resolve.sync(__filename, "#d");
The content of the ./dist/b.mjs file:
import resolve from "enhanced-resolve";
import { fileURLToPath } from "url";
import { join } from "path";

const __filename = fileURLToPath(import.meta.url);
resolve.sync(join(__filename, "../.."), "#d");
The content of the ./dist/c.mjs file:
import "#d";
The content of the ./dist/d.mjs file:
console.log(1);

​./dist/a.mjs will fail and./dist/a.mjs will succeed. ​According to the imports field specification,./dist/a.mjs will succeed and./dist/b.mjs will fail.

project % node dist/a.mjs 
project/node_modules/enhanced-resolve/lib/Resolver.js:293
                        const error = new Error("Can't " + message);
                                      ^

Error: Can't resolve '#d' in 'project/dist/a.mjs'

project % node dist/b.mjs
project % node dist/c.mjs
1

​The relative path condition may be wrong. enhanced-resolve/lib/ImportsFieldPlugin.js

	if (
		// When the description file is inherited from parent, abort
		// (There is no description file inside of this package)
		request.relativePath !== "." ||
		request.request === undefined
	)
		return callback();

mo36924 avatar Nov 26 '20 10:11 mo36924

Try to do it without webpack

alexander-akait avatar Nov 26 '20 11:11 alexander-akait

This is the result of running without a bundler such as webpack or rollup.

mo36924 avatar Nov 26 '20 11:11 mo36924

/cc @vankop

alexander-akait avatar Nov 26 '20 11:11 alexander-akait

Ran into this as well, seems like subpath imports are not resolved when package.json is in a parent directory relative to the module making the request. Node.js works fine on this case. I'm guessing a "package" should be considered to include its subdirectories if they do not contain package.json fields themselves?

strax avatar Apr 13 '21 10:04 strax

@sokra could you take a look on initial problem? should this work?

vankop avatar Apr 13 '21 13:04 vankop