0

After switching from CommonJS to esnext module, import of other .ts files from .ts file is no longer possible.

My getButton.ts imports this:

import { Button } from "../../../objects/Button";

ts.config:

{
    "extends": "..",
    "compilerOptions": {
        "module": "esnext",
        "types": [
              ....
        ],
        "target": "esnext"
    },
}

package.json has:

"type": "module"

So now when I try to run this, I get:

Error: Cannot find module /../../Button
...
 at ESMLoader.resolve

When I provide the full path with .ts extension, I get:

An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

I would like to avoid adding .ts extension to all files, since it cause many other errors to appear, is it possible to solve this by some other way?

John
  • 230
  • 2
  • 12
  • _"no longer supports files without extension"_ - that's not what the last error says. Please make sure the file is in the correct place? – evolutionxbox Aug 21 '23 at 08:53
  • Last error happens after I provide .ts extension, only way to make it work without errors is to give .js extension of the actual .ts file, but I am not sure if that solution is good, has some limitations? As mentioned, is there a way to keep files without extension? – John Aug 21 '23 at 08:59

1 Answers1

-1

In literally says
An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

Disable 'allowImportingTsExtensions' in tsconfig.json

Dimava
  • 7,654
  • 1
  • 9
  • 24
  • But I do not have 'allowImportingTsExtensions' in my tsconfig.json. After adding it many other errors appear. – John Aug 21 '23 at 09:55