enhanced-resolve
enhanced-resolve copied to clipboard
Can't resolve imports field
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();
Try to do it without webpack
This is the result of running without a bundler such as webpack or rollup.
/cc @vankop
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?
@sokra could you take a look on initial problem? should this work?