Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't resolve imports field #265

Open
mo36924 opened this issue Nov 26, 2020 · 5 comments
Open

Can't resolve imports field #265

mo36924 opened this issue Nov 26, 2020 · 5 comments

Comments

@mo36924
Copy link

mo36924 commented Nov 26, 2020

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();
@alexander-akait
Copy link
Member

Try to do it without webpack

@mo36924
Copy link
Author

mo36924 commented Nov 26, 2020

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

@alexander-akait
Copy link
Member

/cc @vankop

@strax
Copy link

strax commented Apr 13, 2021

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?

@vankop
Copy link
Member

vankop commented Apr 13, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants