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

Trailing slashes in the parent are handled differently from import.meta.resolve #30

Closed
cjpearson opened this issue Feb 7, 2025 · 4 comments

Comments

@cjpearson
Copy link

When the parent URL is a directory without a trailing slash, the resolve starts at the parent's parent, but import.meta.resolve uses the parent still.

A test script run from the root of this repo shows the behavior

import { pathToFileURL } from 'url'
import { resolve } from 'import-meta-resolve'

const base = pathToFileURL(process.cwd())
const baseWithTrailing = pathToFileURL(process.cwd() + "/")

console.log(import.meta.resolve('c8', base)) // returns ./node_modules/c8
try {
    console.log(resolve('c8', base)) // throws ERR_MODULE_NOT_FOUND
} catch (e) {
    console.error(e)
}

console.log(import.meta.resolve('c8', baseWithTrailing)) // returns ./node_modules/c8
console.log(resolve('c8', baseWithTrailing)) // returns ./node_modules/c8
@cjpearson
Copy link
Author

My bad, I was missing the --experimental-import-meta-resolve flag.

@wooorm
Copy link
Owner

wooorm commented Feb 7, 2025

weird, I didn’t know there is an import.meta.resolve without that flag too

@wooorm
Copy link
Owner

wooorm commented Feb 7, 2025

Ah, so, currently: without --experimental-import-meta-resolve, parent is ignored. With the flag, parent is accepted. That was the difference.

FYI: You have to pass a URL to a file: https://github.com/wooorm/import-meta-resolve. So no process.cwd(). resolve works from files!
Specifically, almost always, pass import.meta.url, as shown in the readme!

@cjpearson
Copy link
Author

Specifically, almost always, pass import.meta.url, as shown in the readme!

Ah unfortunately it's not my code, but another package I'm using that depends on import-meta-resolve :( But I appreciate the info!

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

2 participants