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.

import.meta.url showing an extension path not the script path

See original GitHub issue

I’ve reproduced on the latest version of NW.js and I’ve searched for import.meta.url to no results.

I am not sure if this is technically possible with how NW.js works, but currently import.meta.url prints a path into a web extension as opposed to a file system path of the script. If this is possible to be somehow intercepted and converted to the file system path, that would be very useful as it would provide an ESM-canonical way of figuring out the current script’s path on the file system. Thank you

NWJS Version : nwjs-sdk-v0.54.1-osx-x64 Operating System : macOS

Expected behavior

import.meta.url should return a file:/// path.

Actual behavior

import.meta.url returns chrome-extension://dicgdfmjdcflpdchdnbjfppblikeeofl/index.js.

How to reproduce

  • Download the latest SDK
  • Copy it to a new folder
  • Create index.html referencing index.js and package.json with index.html for main
  • Add this to index.js:
    window.addEventListener('load', () => {
      document.body.append(import.meta.url);
    });
    
  • Run nwjs-sdk-v0.54.1-osx-x64/nwjs.app/Contents/MacOS/nwjs .
  • See the incorrect path printed:
image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
frank-dspeedcommented, Aug 4, 2021

@TomasHubelbauer the chrome app and extension url is what enables the whole chrome apps and extension api support in your main background Page.

You get access to all internal API’s like Screen and so on. You can Modify any URL and so on for example you can even disable cors via that as you can intercept requests and modify headers.

1reaction
TomasHubelbauercommented, Aug 3, 2021

I see. I also tried __dirname and __filename to see if they maybe are injected into the browser context like require is, but they do not seem to be. Just putting this out here if anyone finds it useful in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How should we handle import.meta #6719 - GitHub
As a developer, I would expect the import.meta.url to contain a path ending with the filename (e. g. the name of the file...
Read more >
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 >
Relative path with import.meta.url - Stack Overflow
Instead of applying path.resolve to import.meta.url , you should specify the relative path as the first parameter such as below
Read more >
Alternatives to __dirname in Node.js with ES modules
To get __dirname (and __filename ) back, you can add code like this to the top of any file that needs it: import...
Read more >
Working with file system paths and file URLs on Node.js - 2ality
In this blog post, we learn how to work with file system paths and file URLs on Node.js.
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