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

support import.meta.dirname and import.meta.filename #18320

Open
EricMCornelius opened this issue Apr 13, 2024 · 3 comments
Open

support import.meta.dirname and import.meta.filename #18320

EricMCornelius opened this issue Apr 13, 2024 · 3 comments

Comments

@EricMCornelius
Copy link

Bug report

#17931 introduced functionality to support __dirname and __filename shims for module output.

Unfortunately, there are false assumptions in the behavior - namely any import.meta.url that has a query param will break.

https://github.com/webpack/webpack/pull/17931/files#diff-ec15f6ea9baa75e9b3ca10449f3b9b7e04e56828330aa5a31d8a83aadd733ed0R225

https://github.com/webpack/webpack/blame/e97af9b5317bc0e7fdbc035b98a577edfe258b83/lib/NodeStuffPlugin.js#L230

What is the current behavior?

Current behavior just string appends '/..' to the import.meta.url value to try to get a directory. This is inaccurate behavior for any import urls which contain query params.

If the current behavior is a bug, please provide the steps to reproduce.

Don't have an easy example I can generate offhand here, but any webpack node build with __dirname: 'node-module' is prone to this issue when running, e.g., const x = import('./relative_path.mjs?reload=123');

What is the expected behavior?

Other relevant information:
webpack version: 6.91.0
Node.js version: 20.11.1
Operating System: linux/windows x64
Additional tools:

@alexander-akait
Copy link
Member

I want to say it is expected, __dirname !== "import.meta.url" (with /..), so that is why you don't query, __dirname === import.meta.dirname (node.js 21 and we will improve it soon, if you browserslist contains node 21)

Try this:

index.mjs

const x = import('./test.mjs?reload=123');

test.mjs

console.log(import.meta.url);
console.log(import.meta.dirname);

@alexander-akait
Copy link
Member

/cc @vankop I think we need to start support import.meta.dirname and import.meta.filename, there are two improvements:

  1. Use import.meta.dirname and import.meta.filename instead __webpack_fileURLToPath__(import.meta.url + "/..") if target/browserslist support it
  2. Don't output undefined for import.meta.dirname and import.meta.filename, i.e. output them as is if they supported or replace on our shim with __webpack_fileURLToPath__(import.meta.url + "/..") (I think we need options here i.e. node.importMetaDirname and node.importMetaFilename)

@alexander-akait alexander-akait changed the title Invalid __dirname resolution technique for node-module when import.meta.url contains query params support import.meta.dirname and import.meta.filename Apr 16, 2024
@inoyakaigor
Copy link

For anyone who need a temporarily solution.

Change

import.meta.dirname

to

path.dirname(process.argv[1]) //*

* In my case there is a script path of my nodejs app. In your case there can be another file path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Priority - Medium
Development

No branches or pull requests

3 participants