#Imports not working correctly in typescript project

5 messages · Page 1 of 1 (latest)

supple patrol
#

I am using typescript in a monorepo setup
And somehow only way to import typescript files is to use '.js' extension.
I understand this is related to compiler options and tsconfig but unable to figure out whats happening.
tsconfig

{
  "extends": "../tsconfig.settings.json",
  "include": ["src"],
  "compilerOptions": {
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "target": "ESNext",
    "outDir": "dist",
    "baseUrl": "src",
    "composite": true,
    "strict": false,
    "paths": {
    },
    "noEmit": false,
  },
  "references": [
    {
      "path": "../configs"
    },
  ]
}

tsconfig.settings

{
  "compilerOptions": {
      "incremental": true,
      "target": "es6",
      "module": "NodeNext",
      "moduleResolution": "NodeNext",
      "lib": [
        "dom",
        "ESNext"
      ],
      "allowJs": true,
      "checkJs": true,
      "composite": true,
      "noEmit": false,
      "declaration": true,
      "declarationMap": true,
      "sourceMap": true,
      "skipLibCheck": false,
      "strict": true,
      "noImplicitAny": false,
      "strictNullChecks": true,
      "useUnknownInCatchVariables": false,
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strictPropertyInitialization": false,
      "types": [
        // "jest",
        // "node"
      ]
    },
    
}

Working import statement:

import { SomeMethod } from "../../../types/common.js";

While using .ts gives An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.ts(5097)

Removing .ts gives Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../../../types/common.js'?ts(2835)

Appreciate any help.

#

package.json has type as module

young rover
#

@supple patrol Importing with the .js extension is correct.

#

That's how ESM works

supple patrol
#

@young rover thanks for confirming 👍