Can't import TypeScript modules without providing the file extension
Asked Answered
R

2

20

I am very new to TypeScript and I expect I should be able to import my TS files without needing to specify that they are TS files.

I have to do

import {sealed} from "./decorators/decorators.ts";

instead of what i want think is the right way which is

import {sealed} from "./decorators/decorators";

which results in errors indicating that it is only looking for files ending .js or .jsx

my tsconfig.json looks like this

{
"compileOnSave": true,
"compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "jsx": "react",
    "allowJs": true,
    "target": "es6",
    "removeComments": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
},
"exclude": [
    "node_modules",
    "typings/browser",
    "typings/browser.d.ts"
]

}

Realize answered 8/4, 2016 at 22:58 Comment(2)
I see your compiling your modules into commonjs modules. What are you using to load them? Webpack? Or is this a node application?Kelso
I am using WebpackRealize
K
19

If you are using webpack try adding this to your webpack.config.js file:

resolve: {
  extensions: ['', '.js', '.jsx', '.ts', '.tsx']
},
Kelso answered 9/4, 2016 at 4:16 Comment(1)
D'oh, that is definitely it. Thanks!Realize
C
7

Answer from thitemple worked for me, but I had to remove '' from the array because it stopped webpack from starting.

resolve: {
  extensions: ['.js', '.jsx', '.ts', '.tsx']
},
Chadburn answered 2/7, 2018 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.