Skip to content

Launching main ESM module with search and hashΒ #49204

Closed as not planned
Closed as not planned
@LiviaMedeiros

Description

@LiviaMedeiros

What is the problem this feature will solve?

We can provide query and hash in ESM imports:

// 1.mjs
import './2.mjs?key=value#hash';
// 2.mjs
const { search, hash } = new URL(import.meta.url);
console.log({ search, hash });
$ node 1.mjs
{ search: '?key=value', hash: '#hash' }

However, AFAICT there is no way to provide them directly to main module:

$ node 2.mjs
{ search: '', hash: '' }

What is the feature you are proposing to solve the problem?

Command line option(s) to provide search and hash, e.g.

$ node 2.mjs --import-search="?key=value" --import-hash="#hash"
or
$ node 2.mjs --esm-appendix="key=value#hash"

What alternatives have you considered?

Right now we can do this with empty script:

$ node --input-type="module" --import="./2.mjs?key=value#hash" < /dev/null
{ search: '?key=value', hash: '#hash' }

$ node --import="./2.mjs?key=value#hash" --eval=";"
{ search: '?key=value', hash: '#hash' }

Which

  • potentially will mess up with import.meta.isMain
  • messes up with process.argv because process.argv[1] will contain first argument instead of path to 2.mjs
  • requires to provide URL, i.e. to percent-encode special characters, to start relative paths with . or .., and to use forward slashes on Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.feature requestIssues that request new features to be added to Node.js.stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions