'TypeError: Unknown file extension ".ts" in a Solana Anchor project

I got this error after updating Solana-cli to 1.9.4 and Anchor-cli to 0.20.1 and other local npm versions...

Local npm package: @project-serum/anchor 0.18.2
Solana program dependencies: anchor-lang 0.18.2, anchor-spl 0.18.2, solana-program 1.9.4
Global environment: Rust 1.57.0, solana-cli 1.9.4, @project-serum/anchor-cli 0.20.1

Anchor.toml [script] command: This command works fine at the the other repo:

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 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 enter image description here



Solution 1:[1]

since test command works other repo, meaning that you already have yarn and ts-node in your system.

tsconfig.json file config.json should be like this:

{
  "compilerOptions": {
    "types": ["mocha", "chai"],
    "typeRoots": ["./node_modules/@types"],
    "lib": ["es2015"],
    "module": "commonjs",
    "target": "es6",
    "esModuleInterop": true
  }
}

Since you are importing a file, "esModuleInterop": true this has to be set. This allows the inference of types from modules.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yilmaz