21

I've been working on unit-testing some code, and out of seemingly nowhere, I got an error about unknown file extensions (before, I got errors about how some type wasn't recognized).

Here is my package.json:

{
  "name": "cgt-core-engine-mv",
  "description": "API container for other CGT plugins",
  "version": "1.01.03",
  "author": "CG-Tespy",
  "repository": {
    "type": "git",
    "url": "https://github.com/CG-Tespy/CGT_CoreEngine_MV"
  },
  "scripts": {
    "build": "tsc & (rollup -c)",
    "test": "mocha -r ts-node/register Test/**/*.ts"
  },
  "dependencies": {},
  "devDependencies": {
    "@types/chai": "^4.2.12",
    "@types/mocha": "^8.0.0",
    "@types/sinon": "^9.0.4",
    "chai": "^4.2.0",
    "mocha": "^8.0.1",
    "rollup": "^2.00.1",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-pluginutils": "^2.8.2",
    "sinon": "^9.0.2",
    "ts-node": "^8.10.2",
    "typescript": "^3.9.6"
  },
  "type": "module"
}

Here is my tsconfig.json:

{
    "compilerOptions": {
      "target": "es5",
      "module": "CommonJS",
      "esModuleInterop": true,

      "sourceMap": false,
      "downlevelIteration": true,
    },
    "include": ["_MainSource/**/*", "Tests/**/*", "@typeDefs/**/*"],
}

I really have no idea where this came from. I have a separate project where I was able to unit-test things just fine, and it didn't give me any errors. The package.json for it:

{
  "name": "unittesting",
  "version": "1.0.0",
  "description": "For practicing unit-testing",
  "main": "main.js",
  "scripts": {
    "test": "mocha -r ts-node/register test/**/*.ts"
    
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/chai": "^4.2.11",
    "@types/mocha": "^8.0.0",
    "chai": "^4.2.0",
    "mocha": "^8.0.1",
    "ts-node": "^8.10.2",
    "typescript": "^3.9.7"
  },
  "type": "module"
}

Its tsconfig:

{
  "compilerOptions": {
    /* Basic Options */

    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "CommonJS",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    
    "esModuleInterop": true,
    
  },
  "include": ["src/**/*", "tests/**/*"],
}

What could be causing this issue? I've tried the suggestions here, but it didn't work (even after looking at the Github issue linked).

Tespy
  • 971
  • 2
  • 9
  • 18
  • do you have the same issue if you drop the `type: module` in package.json? – rags2riches-prog Aug 16 '20 at 15:57
  • 2
    @rags2riches After doing that, I instead get an error about how I can't use the import statement outside a module. – Tespy Aug 17 '20 at 21:09
  • 2
    @Tepsy ditto. It's basically an ever ending loop and I cannot understand why. I am still investigating it. – rags2riches-prog Aug 17 '20 at 21:16
  • 1
    Does this answer your question? [Can't run my Node.js Typescript project TypeError \[ERR\_UNKNOWN\_FILE\_EXTENSION\]: Unknown file extension ".ts" for /app/src/App.ts](https://stackoverflow.com/questions/62096269/cant-run-my-node-js-typescript-project-typeerror-err-unknown-file-extension) – Viliam Jobko Nov 27 '20 at 20:04
  • 2
    @ViliamJobko No, it doesn't – Tespy Dec 09 '20 at 21:16
  • Something *similar* works for me with `"test-no-coverage": "ts-mocha --loader=ts-node/esm './src/*.spec.ts'"`, details are different, please see: https://github.com/andreashuber69/verify-coldcard-dice-seed/blob/develop/package.json – Andreas Dec 25 '22 at 18:14
  • changing `"module": "CommonJS"` to `"module": "ES2015"` in `tsconfig.json` worked for me – Balder May 07 '23 at 07:03

2 Answers2

0

this issue happens when you add "type": "module", in package.json file while working with node typescript.

eliassn
  • 39
  • 5
-9

Have you tried removing "type": "module" from package.json file

  • 29
    As I explained in a comment response to the OP, doing that causes an error about how I can't use import statements outside modules – Tespy Sep 11 '20 at 00:00
  • thanks, I wasn't looking in the right direction, you saved me some headache :) – Utopik May 06 '21 at 12:31