Skip to content

import.meta.url support #1492

@GenrikhFetischev

Description

@GenrikhFetischev

Hi!
I have a problem with the bundling yargs package. When I try to run bundled code I get the following error:

node:internal/url:1352
    throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined
    at new NodeError (node:internal/errors:278:15)
    at fileURLToPath (node:internal/url:1352:11)
    at Object.<anonymous> (/dir/b.js:524035:49)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Looks like it happens cause of the following code (github):

const mainFilename = fileURLToPath(import.meta.url).split('node_modules')[0]
const __dirname = fileURLToPath(import.meta.url)

Which transpiled into:

var import_meta = {};
var mainFilename = (0, import_url.fileURLToPath)(import_meta.url).split("node_modules")[0];
var __dirname = (0, import_url.fileURLToPath)(import_meta.url);

import_meta.url - obviously undefined there.

I try to make a bundle for node.js with the flag --format=cjs

I have checked out this issue - #208, but looks like my isn't duplicate because I try to bundle ESM -> CJS;

Is it possible to add support import.meta.url property for ESM -> CJS bundle?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions