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

ts-node error on build attempt #1926

Closed
tomasmenezes opened this issue Nov 10, 2023 · 2 comments · Fixed by #1977
Closed

ts-node error on build attempt #1926

tomasmenezes opened this issue Nov 10, 2023 · 2 comments · Fixed by #1977

Comments

@tomasmenezes
Copy link

tomasmenezes commented Nov 10, 2023

I was trying to build the registry locally with the latest pnpm version but kept running into the 'Unknown file extension ".ts"' for "build-registry.ts" error (even with the passed esm flag). Also reproduced easily when creating a codespace on the main repo.

Full output for pnpm build:registry (or main build)

> ui@0.0.1 build:registry /ui
> pnpm --filter=www build:registry
> www@0.0.1 build:registry /ui/apps/www
> ts-node --esm --project ./tsconfig.scripts.json ./scripts/build-registry.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts
    at new NodeError (node:internal/errors:406:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
    at defaultLoad (node:internal/modules/esm/load:120:20)
    at nextLoad (node:internal/modules/esm/hooks:833:28)
    at load (/ui/node_modules/.pnpm/ts-node@10.9.1_@types+node@17.0.45_typescript@4.9.5/node_modules/ts-node/dist/child/child-loader.js:19:122)
    at nextLoad (node:internal/modules/esm/hooks:833:28)
    at Hooks.load (node:internal/modules/esm/hooks:416:26)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:168:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
/ui/apps/www:
 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  www@0.0.1 build:registry: `ts-node --esm --project ./tsconfig.scripts.json ./scripts/build-registry.ts`
Exit status 1
 ELIFECYCLE  Command failed with exit code 1.
@Gravy59
Copy link
Contributor

Gravy59 commented Nov 12, 2023

Hi,
This affected me as well. It seems to be an issue with ts-node and ESM, and fixing it may require removing "type": "module" from package.json. TypeStrong/ts-node#1062, TypeStrong/ts-node#2033, and TypeStrong/ts-node#1997 show a recurring pattern.

What I did to work around this was install tsx as a devDependency. tsx is an alternative typescript runtime for node, and it worked flawlessly for me.

pnpm add -Dw tsx
cd apps/www
pnpm tsx --tsconfig ./tsconfig.scripts.json ./scripts/build-registry.ts

Please let me know if this works for you. If it does, I will open a PR suggesting the switch.

@tomasmenezes
Copy link
Author

@Gravy59 That seems to do the trick! Thank you

@kodiakhq kodiakhq bot closed this as completed in #1977 Jan 28, 2024
kodiakhq bot pushed a commit that referenced this issue Jan 28, 2024
…1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
kjxbyz pushed a commit to muse-ui/muse-ui that referenced this issue Jun 7, 2024
…hadcn-ui#1977)

This pull request resolves shadcn-ui#1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in shadcn-ui#1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on shadcn-ui#1937. That will not happen again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants