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.url is undefined for CommonJS output, causing TypeError [ERR_INVALID_ARG_VALUE] #187

Open
exoego opened this issue Mar 19, 2024 · 0 comments

Comments

@exoego
Copy link

exoego commented Mar 19, 2024

Thanks for quick fixing #164
Now I am able to esbuild a code using quickjs-emscripten that leverages new URL(..., import.meta.url).

However, I noticed that the built JS throws a runtime error: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined.

The below is a part of generated JS.

var import_meta, QuickJSRaw, emscripten_module_default;
var init_emscripten_module = __esm({
  "node_modules/quickjs-emscripten/node_modules/@jitl/quickjs-wasmfile-release-sync/dist/emscripten-module.mjs"() {
    import_meta = {};
                  ^^ "url" is undefined here !!
//
// snip
//
        if (y2) {
          const { createRequire: a3 } = await import("module");
          var require2 = a3(import_meta.url), fs = require2("fs"), D2 = require2("path");
                            ^^^^^^^^^^^^^^^ crash here !!

This error happens in CommonJS build.
I need to use CommonJS build since some other dependencies does not work with ESM for other reasons.

If I change the build to ESM, the generated artifact run successfully since import.meta.url is preserved and ESM supports it.

-     outfile: "dist/index.js",
+     outfile: "dist/index.mjs",
+     format: "esm",

Expectation

CommonJS mode is supported

Actual

A runtime error TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined is thrown, since import_meta.url is undefined.

Repro

git clone  --branch=import-meta-url https://github.com/exoego/quickjs-emscripten-esbuild-repro.git
cd quickjs-emscripten-esbuild-repro
npm i
npm run build
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