question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TypeScript] Unknown file extension ".ts"

See original GitHub issue

I have issues running express-openapi in a basic TypeScript project run through ts-node this way: node -r ts-node/register, this happens when the framework is trying to import my paths .ts files on this line: https://github.com/kogosoftwarellc/open-api/blob/9e8204657c656410aeafff9249834f66af6f97c3/packages/openapi-framework/index.ts#L238

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Worth mentioning, I am ~using~ trying to use OpenAPI 3.0.2 (a lot of issues with the current JSON schema used here but this is another topic) and run Node 16.

I have made a CodeSandbox reproducing this issue:

Step to reproduce:

  1. Go to ~https://codesandbox.io/s/express-openapi-typescript-4w91hl~

    _EDIT: New sandbox with less noise and auto-reload for easier debug (you can skip step 2.): https://codesandbox.io/s/express-openapi-ts-node-we8kqn_

  2. ~Restart Server (Left Menu: Server Control Panel > Control Container > Restart Server)~
  3. Read the output in the default terminal

And even while trying to workaround while building for production, I cannot access any endpoint nor api-docs:

Step to reproduce:

  1. Go to https://codesandbox.io/s/express-openapi-typescript-4w91hl
  2. Start new Terminal with the “+” button
  3. Run npm run build (or npm run build:watch if you want to play around)
  4. Start new Terminal
  5. Run npm run start:prod
  6. Use the browser to navigate to:
    • / => OK - {"foo": "bar"} (Express parent App is working)
    • /api/healthcheck => OK - 200 (Express Router is working)
    • /api/api-docs => KO (/api-docs is not served)
    • /api/test => OK - {"path": "test"} (Express child App is working)
    • /api/users/xyz => KO (/users/{id} is not served)

Can someone help me figure out what I should do to make this work? 🙏 Or maybe someone has a current working example of TypeScript project using this lib?

Thanks in advance for your insights!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
cspotcodecommented, Jul 20, 2022

This seems to be the problem:

this happens when the framework is trying to import my paths .ts files on this line:

const imported = await import(`file://${fsRoutesItem.path}`); 

The framework uses import() and gives it a path to a TS file, sorta like this: import('./path/to/ts/file.ts')

Even when the file being loaded is a CommonJS file, import() goes through node’s internal ESM codepaths. Those codepaths can load CJS files, so it’s not changing the interpretation of your code from CJS to ESM. That is not the problem: your code is still CJS.

However, node’s ESM codepaths do not understand new file extensions. The only way to teach it about new file extensions, is to use ts-node’s ESM loader. That is the only way to plug into node’s internal ESM stuff.

This requires us to pass the --loader CLI flag to node, which means spawning a subprocess. Node’s loaders API is also marked experimental (you can read about the definition of “experimental” in node’s documentation)

For these reasons, we do not force projects to use an experimental API; you have to opt-in. ts-node-esm will opt-in. It’s an alias for ts-node --esm, and you can also specify "esm": true in your tsconfig.

1reaction
cspotcodecommented, Jul 20, 2022

Excellent, happy to help. My time is limited so if you have any future questions, I recommend asking on the TypeScript Community Discord server. The server has a great help system with plenty of experts willing to offer advice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't run my Node.js Typescript project TypeError ...
Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts · Ask Question.
Read more >
Error: unknown file extension .ts · Issue #1062 - GitHub
When I try to execute a typescript file I get the following error message: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ...
Read more >
[err_unknown_file_extension]: unknown file extension ".ts" for
this issue happens when you add "type": "module", in package.json file while working with node typescript. Open side panel. TypeError [ ...
Read more >
[SOLVED] Unknown File Extension ".ts" in ts-node
Learn how to easily solve the "Unknown file extension '.ts'" error (ERR_UNKNOWN_FILE_EXTENSION) when using ts-node to run a TypeScript file.
Read more >
ts-node - npm
How it works; Ignored files. File extensions; Skipping node_modules; Skipping pre-compiled TypeScript; Scope by directory; Ignore by regexp.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found