question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make import.meta.url dynamic

See original GitHub issue

Bug report

What is the current behavior?

import.meta.url is replaced by the current file location at the build time. https://github.com/webpack/webpack/blob/main/lib/dependencies/ImportMetaPlugin.js#L99

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

If you build

console.log(import.meta.url)

it will ouput a constant string (le location of the file on the current system).

This is problematic to build a project using yargs (https://github.com/yargs/yargs), as it uses meta.import.url (https://github.com/yargs/yargs/blob/main/lib/platform-shims/esm.mjs#L18).

When building under an Unix file system, meta.import.url is replaced by something like file:///path/to/your/project/src/index.js. When trying to run the built code under a Windows filesystem, a TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute is thrown, as the filename doesn’t contain file://C:/....

Making the meta.import.url variable dynamic (based on __dirname) should solve the problem.

What is the expected behavior?

import.meta.url should be based on __dirname for cjs build, to keep the original intended behavior of the application developer.

Other relevant information: webpack version: 5.58.1 Node.js version: 16.10.0 Operating System: Ubuntu Linux 20.04 Additional tools: yargs

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Congelli501commented, Oct 11, 2021

It is the build time location of the file and not the run time dynamic location of the JS file

0reactions
pavelsavaracommented, Jan 25, 2022

Hi all, I tried to contribute new configuration option to fix this, but I struggle to make all tests work. I would appreciate some help.

Also I wonder what is the use-case for the current behavior, with the file name from build time. Could somebody advise ? @vankop

Read more comments on GitHub >

github_iconTop Results From Across the Web

import.meta - JavaScript - MDN Web Docs
The import.meta meta-property exposes context-specific metadata to a JavaScript module. It contains information about the module, ...
Read more >
babel/plugin-syntax-import-meta
NOTE**: This plugin is included in `@babel/preset-env`, in [ES2020](https://github.com/tc39/proposals/blob/master/finished-proposals.md)
Read more >
Using a full URL in a dynamic import() - Stack Overflow
ES2020 introduces a new function-like syntax for import, so-called "dynamic imports" permitting the dynamic import of JavaScript modules.
Read more >
Build Options - Vite
The list of chunks to preload for each dynamic import is computed by Vite. ... import.meta.url is used at runtime to avoid absolute...
Read more >
URL Imports - next.config.js
URL imports are an experimental feature that allows you to import modules directly from external servers (instead of from the local disk).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found