TypeError: Unknown file extension ".ts" - esmodules support

See original GitHub issue

Original post in Anchor repo: [(https://github.com/project-serum/anchor/issues/1286)]

Anchor.toml [script] command: test = “yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts”

The problem is the m1.ts file cannot import functions from another ts file!!!??? in my utils.ts

export const log1 = console.log;

in my m1.ts file:

import { log1 } from './utils';
describe('scenario1', async () => {
  it('initialize', async () => {
    log1('\n---------== init');
  });
});

the imported log1 function or any other function will cause the Unknown file extension “.ts” error!!??

my local package dependencies: “mocha”: “^9.1.3”, “ts-mocha”: “^9.0.0-alpha1”, “ts-node”: “^10.4.0”, “typescript”: “^4.5.4”

Please advise. Thank you unknown file extension ts2

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:27 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
manooogcommented, Aug 17, 2022

Hello everyone,I have the same problem and I think the problem was caused by tsconfig.json

// ❌ when module = esnext
{
	"compilerOptions": {
    	"module": "esnext"
    }
}
// ✅ change module to commonjs,then everything is ok
{
	"compilerOptions": {
    	"module": "CommonJS"
    }
}

Hope this trick can help.

5reactions
cn-iacommented, Feb 20, 2022

Try ts-mocha -n loader=ts-node/esm -p tsconfig.json tests/**/*.test.ts via npm scripts, it works for me! (Optional) Check npx ts-mocha --help for more details of this option.

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
Expected Behavior Load typescript files Actual Behavior When I try to execute a typescript file I get the following error message: TypeError ......
Read more >
ts node unknown file extension ".ts" - You.com | The Search ...
My setup: I run my code in Node 16.13.2; I build with Gulp. Build is successful and I can run my code with...
Read more >
ts-node - npm
This error is thrown by node when a module has an unrecognized file extension, or no extension at all, and is being executed...
Read more >
ES Modules in NodeJS - Troubleshooting Resources
Node runtime powered by esbuild, supports TS and ESM. See my tooling section in my TS ... Make sure all imports include file...
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