folke / esbuild-runner

⚡️ Super-fast on-the-fly transpilation of modern JS, TypeScript and JSX using esbuild
https://www.npmjs.com/package/esbuild-runner
Apache License 2.0
711 stars 24 forks source link

ERR_UNKNOWN_FILE_EXTENSION running a basic ts file? #36

Open cjroebuck opened 2 years ago

cjroebuck commented 2 years ago

Not sure what I'm doing wrong but...

node -r esbuild-runner/register helloworld.ts 
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/cjr/dev/packages/api/helloworld.ts
    at new NodeError (node:internal/errors:363:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:105:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
    at Loader.import (node:internal/modules/esm/loader:177:17)
    at Object.loadESM (node:internal/process/esm_loader:68:5)

helloworld.ts:

console.log("HELLO WORLD LITERALLY!");
export default {}
node -v
> v16.3.0
cjroebuck commented 2 years ago

Should add, this is within an ESM module where "type":"module" is present in package.json.

And tsconfig.json for this is:

{
  "compilerOptions": {
    "moduleResolution": "Node",
    "skipLibCheck": true,
    "lib": ["es2020"],
    "module": "ES2020",
    "target": "es6",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}
sushruth commented 2 years ago

I can confirm that this has been happening to me in a yarn workspace as well, even with esr ./my/file.ts

EDIT: I found a repeatable way to repro this in yarn 2+ - if for some reason yarn pnpEnableEsmLoader: true is set in .yarnrx.yml, this always happens. In case this is not set, if @yarnpkg/cli is part of the yarn workspace tree, this happens then as well.

EDIT 2: Turns out, the esm loader is the one that is screwing it up for me. This is what I had to configure in my yarn 2 workspace (.yarnrc.yml) to get esbuild-runner to work -

enableGlobalCache: false
pnpEnableEsmLoader: false

That made esr work for me.

revmischa commented 2 years ago

I'm getting ERR_UNKNOWN_FILE_EXTENSION as well using esr in my ESM project, using NPM.

codetheweb commented 2 years ago

https://github.com/antfu/esno seems to work with ES modules.

marklai1998 commented 2 years ago

why is it still not solved after half a year

codetheweb commented 2 years ago

Yep, I'm using tsx as well now and it's been working great.

marklai1998 commented 1 year ago

Moved to tsx as well

Toilal commented 1 year ago

Did you find a way to add esbuild configuration using tsx ? (something like esbuild-runner.config.js file would be great)

c-collamar-awsys-i commented 1 month ago

I gave this package a shot today and was immediately greeted with this very issue. I was intrigued as to why there are no signs of this getting fixed after almost 3 years, until I realized this package's latest development was from 2 years ago!

I assume this package as unmaintained.