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

TypeError: Unknown file extension ".ts" | #71

Closed
Jason-Terry opened this issue Jan 4, 2024 · 12 comments
Closed

TypeError: Unknown file extension ".ts" | #71

Jason-Terry opened this issue Jan 4, 2024 · 12 comments

Comments

@Jason-Terry
Copy link

Jason-Terry commented Jan 4, 2024

package.json

  "name": "sd-tracker-api",
  "version": "1.0.0",
  "description": "",
  "main": "main.ts",
  "scripts": {
    "build": "tsc -p .",
    "dev": "SET NODE_ENV=dev & ts-node-dev --respawn --transpile-only -r tsconfig-paths/register src/main.ts",
    "test": "SET NODE_ENV=test & mocha --check-leaks -r ts-node/register -r tsconfig-paths/register tests/**.spec.ts"
  },
  "author": "Jason Terry",
  "license": "ISC",
  "devDependencies": {
    "@types/chai": "^4.3.11",
    "@types/express": "^4.17.21",
    "@types/mocha": "^10.0.6",
    "@types/node": "^20.10.6",
    "@types/supertest": "^6.0.2",
    "chai": "^5.0.0",
    "mocha": "^10.2.0",
    "supertest": "^6.3.3",
    "ts-node": "^10.9.2",
    "ts-node-dev": "^2.0.0",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.3"
  },
  "dependencies": {
    "dotenv": "^16.3.1",
    "express": "^4.18.2"
  }
}

tsconfig.json

{
  "exclude": ["./coverage", "./dist", "./node_modules"],
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "target": "es2016", 
    "module": "Node16",  
    "rootDir": "./src",    
    "moduleResolution": "Node16",
    "baseUrl": "./src",
    "outDir": "./dist",
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "skipLibCheck": true                                 
  }
}

.mocharc.json

{
  "extension": ["ts"],
  "spec": "tests/**/*.spec.ts",
  "require": "ts-node/register"
}

Greetings, I am unable to run mocha when including import { expect } from 'chai'; into my project.

It causes the following to occour.

npm run test

> sd-tracker-api@1.0.0 test
> SET NODE_ENV=test & mocha --check-leaks -r ts-node/register -r tsconfig-paths/register tests/**.spec.ts


TypeError: Unknown file extension ".ts" for E:\my-learning\sd-tracker-api\tests\types.spec.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:141:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21)

I am not sure what to do to solve this? I followed the guidance of the following video along with the TS example project.

Video
Project

Any help would be greatly appreciated.

@huangjihua
Copy link

the same

@Sweet-PotEdos
Copy link

same here!

@AntonSweb
Copy link

same problem

@AntonSweb
Copy link

"loader=ts-node/esm" need to be added to the node-option in the mocha configuration

.mocharc.json

{
  "extension": [
    "ts"
  ],
  "spec": "tests/**/*.spec.ts",
  "require": [
    "ts-node/register"
  ],
  "node-option": [
    "loader=ts-node/esm"
  ],
  "recursive": true
}

@seventeen-yao
Copy link

This question comes from Chat.js 5.x, using 4.x will solve that.
chaijs/chai#1594 (reply in thread)

@sahin52
Copy link

sahin52 commented May 9, 2024

it was related to chai in probably your example
please also check
chaijs/chai#1597

@sahin52
Copy link

sahin52 commented May 17, 2024

I added esm to my command by loader=ts-node/esm, but now I'm getting typescript error which doesn't exist.

@JoshuaKGoldberg
Copy link
Member

As of #76, we now have at least two working examples of using TypeScript with recent tools. You can see `packages/typescript-ts-node-esm-loader. Cheers! ☕

@sahin52
Copy link

sahin52 commented Jul 5, 2024

In the example, package.json -> type is equal to module, but we weren't using that, which means it is a commonjs. The project doesn't work with module. Can you give an example where the package.json type is equal to commonjs? @JoshuaKGoldberg

@JoshuaKGoldberg
Copy link
Member

packages/typescript-tsx-esm-import works on my machine if I switch it to "type": "commonjs" (or remove the "type": property altogether).

@sahin52
Copy link

sahin52 commented Jul 8, 2024

After only removing "type" property in package.json, it did not work. Here is npm run test command result after removing type property

(node:2020) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2020) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

 Exception during run: C:\Users\Sahin\Desktop\Temp\mocha-examples\packages\typescript-ts-node-esm-loader\src\index.spec.ts:1
import { equal } from "assert";
^^^^^^

SyntaxError: Cannot use import statement outside a module

node version: v21.6.2, npm version: 10.2.4

But I changed the file extensions to .mts and changed the imports etc accordingly. It now works with this configuration.
Here is the commit for it: sahin52@ced4f98

@JoshuaKGoldberg
Copy link
Member

This is rapidly descending into becoming a support thread, which GitHub issues on an examples repo are not well suited for. Your best bet would be to ask in one of Mocha's support forums: the Mocha Discord's #help channel or Stackoverflow's mocha tag. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants