Skip to content

Ts-node module resolution is not respected #1858

Closed

Description

@lantos1618

Search Terms

ts-node module type resolution package.json
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /...

Expected Behavior

when "moduleType" is specified in ts-config.json it should ignore the "type": "module" specified in package.json

tsconfig.json

  "ts-node": {
    "moduleTypes": {
      "*": "commonjs"
    }
  },

package.json

{
  "name": "typescript-incorrect-behaviour",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",

Actual Behavior

"type": "module", is respected when tsconfig.json should be respected.

Even passing "NODE_OPTIONS='--import-type=commonjs'" does not get respected.

throws

Debugger attached.
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ~/Desktop/typescript-incorrect-behaviour/src/index.ts
    at new NodeError (node:internal/errors:372:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:76:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:118:38)
    at defaultLoad (node:internal/modules/esm/load:21:20)
    at ESMLoader.load (node:internal/modules/esm/loader:407:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Steps to reproduce the problem

npm init
npm install typescript ts-node @types/node
tsc --init
create a src/index.ts with console.log("hello") inside
add "type": "module" to package.json
add "start": "ts-node src/index.ts" to package.json
npm start

Minimal reproduction

I have an example here.
https://github.com/lantos-lgtm/typescript-incorrect-behaviour

git clone  https://github.com/lantos-lgtm/typescript-incorrect-behaviour
cd typescript-incorrect-behaviour
npm install
npm start

Specifications

ts-node v10.9.1
node v16.16.0
compiler v4.7.4

  • tsconfig.json, if you're using one:
{
  "ts-node": {
    "moduleTypes": {
      "*": "commonjs"
    }
  },
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}
  • package.json:
{
  "name": "typescript-incorrect-behaviour",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "start": "ts-node src/index.ts",
    "start_node_env_override": "ts-node NODE_OPTIONS='--import-type=commonjs' src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "*",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  }
}

  • Operating system and version: macos 12.5
  • If Windows, are you using WSL or WSL2?: none

Activity

  1. cspotcode commented on Aug 8, 2022

    @cspotcode
    Collaborator

    I double-checked the docs, looks like moduleTypes might be configured incorrectly:
    https://typestrong.org/ts-node/docs/module-type-overrides

  2. lantos1618 commented on Aug 8, 2022

    @lantos1618
    Author

    Just updated to cjs still same response

  3. locked and limited conversation to collaborators on Aug 8, 2022
  4. converted this issue into a discussion #1859 on Aug 8, 2022
  5. added
    user errorIssues that were ultimately human error, not a bug nor a missing feature.
    on Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    user errorIssues that were ultimately human error, not a bug nor a missing feature.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions