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

import.meta.resolve returning built-in specifier instead of URL string (when dependency has the same name as a built-in) #56652

Closed
thescientist13 opened this issue Jan 18, 2025 · 1 comment

Comments

@thescientist13
Copy link

Version

23.6.0

Platform

macOS

Subsystem

No response

What steps will reproduce the bug?

  1. Have the package process installed
    $ npm i process
  2. Create a script; index.js
    const resolved = import.meta.resolve('process');
    
    console.log({ resolved });
  3. Run node index.js

How often does it reproduce? Is there a required condition?

Anytime the specifier is also the same name as a NodeJS built in (presumably)

What is the expected behavior? Why is that the expected behavior?

It should return a URL string, as per the spec, eg: file:///path/to/node_modules/process/index.js

What do you see instead?

A specifier for a NodeJS built-in, e.g. node:process

Additional information

I created a repro repo
https://github.com/thescientist13/import-meta-resolve-demo

And some more details
https://github.com/thescientist13/import-meta-resolve-demo?tab=readme-ov-file#node-built-ins-hijacking-dependencies


FWIW, it seems require.resolve exhibits the same behavior

import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const resolved = require.resolve('process');

console.log({ resolved });
➜  import-meta-resolve-demo git:(master) ✗ npm run demo

> import-meta-resolve-demo@1.0.0 demo
> node .

{ resolved: 'process' }
@thescientist13
Copy link
Author

Thanks @ljharb , for guiding me to this part of the NodeJS docs, which confirms this is the expected behavior

Some built-in modules are always preferentially loaded if their identifier is passed to require(). For instance, require('http') will always return the built-in HTTP module, even if there is a file by that name.

@thescientist13 thescientist13 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2025
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

1 participant