Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Unknown file extension ".zx" #1103

Closed
lukakostic opened this issue Feb 20, 2025 · 1 comment
Closed

Unknown file extension ".zx" #1103

lukakostic opened this issue Feb 20, 2025 · 1 comment

Comments

@lukakostic
Copy link

lukakostic commented Feb 20, 2025

Script:

#!/usr/bin/env zx

console.log("Hi");

Ran with:
$ ./my-script.zx

Results in:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".zx" for /data/bin/my-script.zx
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Why does it care about file extensions on linux?
Why cant i end my script file with .zx ?
If im running with the hashbang zx , obviously its a valid zx script (javascript) which im trying to run, so why check extension?
Its not like i can run typescript too so it needs to know js vs ts ?

@antongolub
Copy link
Collaborator

antongolub commented Feb 20, 2025

nodejs/node#23868

// loader.mjs

import path from 'node:path'
const exts = new Set(['.zx'])

export async function resolve(specifier, context, nextResolve) {
  const ext = path.extname(specifier)
  if (!exts.has(ext)) { return nextResolve(specifier) }
  const { url } = await nextResolve(specifier)

  return {
    format: 'zx',
    shortCircuit: true,
    url,
  }
}

export async function load(url, context, nextLoad) {
  if (context.format !== 'zx') { return nextLoad(url) }

  return {
    format: 'module',
    shortCircuit: true,
    source: '' + (await nextLoad(url, { ...context, format: 'module' })).source,
  }
}
 NODE_OPTIONS="--loader=./loader.mjs" ./my-script.zx

@google google locked and limited conversation to collaborators Feb 20, 2025
@antongolub antongolub converted this issue into discussion #1104 Feb 20, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants