Skip to content

ESM: Cannot find module './index.js' and requiring "imaginaryUncacheableRequireResolveScript" #2099

Description

@flleeppyy

Search Terms

If I try to run the script src/index.ts, it results in this error

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/chen/development/relatrack-backend/src/index.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 nextLoad (node:internal/modules/esm/hooks:865:22)
    at async nextLoad (node:internal/modules/esm/hooks:865:22)
    at async Hooks.load (node:internal/modules/esm/hooks:448:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

But if I run the script with script/index.js, I get this error

node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module './index.js'
Require stack:
- /home/chen/development/relatrack-backend/src/imaginaryUncacheableRequireResolveScript
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Function.resolve (node:internal/modules/helpers:187:19)
    at requireResolveNonCached (/home/chen/development/relatrack-backend/node_modules/.pnpm/ts-node@10.9.2_@types+node@20.10.5_typescript@5.3.3/node_modules/ts-node/dist/bin.js:550:16)
    at getProjectSearchDir (/home/chen/development/relatrack-backend/node_modules/.pnpm/ts-node@10.9.2_@types+node@20.10.5_typescript@5.3.3/node_modules/ts-node/dist/bin.js:519:40)
    at phase3 (/home/chen/development/relatrack-backend/node_modules/.pnpm/ts-node@10.9.2_@types+node@20.10.5_typescript@5.3.3/node_modules/ts-node/dist/bin.js:267:27)
    at bootstrap (/home/chen/development/relatrack-backend/node_modules/.pnpm/ts-node@10.9.2_@types+node@20.10.5_typescript@5.3.3/node_modules/ts-node/dist/bin.js:47:30)
    at main (/home/chen/development/relatrack-backend/node_modules/.pnpm/ts-node@10.9.2_@types+node@20.10.5_typescript@5.3.3/node_modules/ts-node/dist/bin.js:33:12)
    at Object.<anonymous> (/home/chen/development/relatrack-backend/node_modules/.pnpm/ts-node@10.9.2_@types+node@20.10.5_typescript@5.3.3/node_modules/ts-node/dist/bin.js:580:5)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/chen/development/relatrack-backend/src/imaginaryUncacheableRequireResolveScript'
  ]
}

Node.js v20.10.0

No where am I importing "imaginaryUncacheableRequireResolveScript" and looking that up on Google yields nothing.

Expected Behavior

It runs my code with either of the file extensions I provide

Actual Behavior

The errors above.

Steps to reproduce the problem

running pnpm/npm ts-node src/index.js with my tsconfig (making sure tpye is set to module in package.json)

Minimal reproduction

https://github.com/flleeppyy/ts-node-repros
TypeStrong/ts-node-repros#37

Specifications

ts-node v10.9.2
node v20.10.0
compiler v5.3.3

  • tsconfig.json, if you're using one:
{
  "compilerOptions": {
    "allowJs": false,
    "allowSyntheticDefaultImports": true,
    "allowArbitraryExtensions": true,
    "alwaysStrict": true,
    "baseUrl": ".",
    "checkJs": false,
    "downlevelIteration": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "incremental": true,
    "inlineSources": false,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": ["ES2023"],
    "module": "NodeNext",
    // "moduleResolution": "Node",
    "noEmit": false,
    "noEmitHelpers": true,
    "noEmitOnError": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": false,
    "noImplicitThis": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "outDir": "./dist",
    "pretty": true,
    "removeComments": true,
    "resolvePackageJsonImports": true,
    "resolvePackageJsonExports": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": false,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "suppressImplicitAnyIndexErrors": false,
    "target": "ES2020",
    "verbatimModuleSyntax": true,
    "useDefineForClassFields": true
  },
  "ts-node": {
    "experimentalSpecifierResolution": "node",
    "transpileOnly": true,
    "esm": true
  }
}

  • package.json:
{
  "name": "relatrack-backend",
  "version": "1.0.0",
  "description": "Backend to Relatrack, an inventory solution.",
  "main": "src/index.ts",
  "type": "module",
  "scripts": {
    "dev": "nodemon src/index.js",
    "start": "cd dist && node index.js",
    "build": "tsc",
    "lint": "eslint . --ext .js,.ts --fix --ignore-path .gitignore && prettier --write --ignore-path .gitignore .",
    "db:genschema": "cross-env node --no-warnings --loader ts-node/esm -r ts-node/register ./src/utils/dbSchemaParser.ts"
  },
  "engines": {
    "node": ">=20.6.0",
    "pnpm": ">=8.0.0"
  },
  "keywords": [],
  "author": {
    "name": "Chen Jinkerson",
    "email": "flleeppyybusiness@gmail.com",
    "url": "https://fleepy.tv"
  },
  "license": "MIT",
  "dependencies": {
    "@fastify/cors": "^8.5.0",
    "@fastify/helmet": "^11.1.1",
    "@fastify/multipart": "^8.0.0",
    "@typescript-eslint/types": "^6.15.0",
    "ajv": "^8.12.0",
    "ajv-keywords": "^5.1.0",
    "bcryptjs": "^2.4.3",
    "better-sqlite3": "^9.2.2",
    "cross-env": "^7.0.3",
    "dotenv": "^16.3.1",
    "fastify": "^4.25.1",
    "fastify-plugin": "^4.5.1",
    "image-size": "^1.0.2",
    "jose": "^5.1.3",
    "json-schema-to-typescript": "^13.1.1",
    "jsonwebtoken": "^8.5.1",
    "knex": "^2.5.1",
    "pino": "^8.17.1",
    "pino-pretty": "^10.3.0",
    "sqlite3": "^5.1.6",
    "ts-node": "^10.9.2",
    "tslib": "^2.6.2"
  },
  "devDependencies": {
    "@espimarisa/eslint-config": "^4.2.0",
    "@types/bcryptjs": "^2.4.6",
    "@types/jsonwebtoken": "^8.5.9",
    "@types/node": "^20.10.5",
    "@typescript-eslint/eslint-plugin": "^6.15.0",
    "@typescript-eslint/parser": "^6.15.0",
    "@typescript-eslint/typescript-estree": "^6.15.0",
    "eslint": "^8.56.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-import-resolver-typescript": "^3.6.1",
    "eslint-plugin-import": "^2.29.1",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^5.0.1",
    "eslint-plugin-unicorn": "^49.0.0",
    "nodemon": "^3.0.2",
    "prettier": "^3.1.1",
    "typescript": "^5.3.3"
  }
}

  • Operating system and version: Ubuntu 23.10 x86_64

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions