Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript, index.d.ts and webpack: Module not found error

Tags:

typescript

I created Typescript project with webpack, following this tutorial. As I have a lot of modules, I added index.d.ts to my src/components folder where I export all my modules:

export {Module1} from "./Module1";
export {Module2} from "./Module2";
export {Module3} from "./Module3";

I added index.d.ts to tsconfig.json files:

"files": [
    "./src/components/index.d.ts"
]

Then, in index.tsx, which is in src folder, I import:

import * as MyModule from "./components/";

Code hinting works fine, so I suppose all paths are OK. However when I run webpack, I get this error:

Module not found: Error: Cannot resolve directory './components'

As I understand, webpack doesn't find this index.d.ts file. I tried adding d.ts to webpack.config.js extensions, like described here, but then I've got a different error message. Am I missing something?

like image 514
zeroin Avatar asked Jul 07 '16 17:07

zeroin


People also ask

How to fix “index d is not a module” error in typescript?

To fix the "index.d.ts is not a module" error when importing the webrtc module with TypeScript, we can set the types property in tsconfig.json to ["webrtc"]. { "compilerOptions": { "target": "es5", "sourceMap": true, "noImplicitAny": true, "types": ["webrtc"] }, "exclude": ["node_modules"] } in tsconfig.json.

How to fix “index is not a module” error when importing WebRTC?

To fix the "index.d.ts is not a module" error when importing the webrtc module with TypeScript, we can set the types property in tsconfig.json to ["webrtc"]. ← → How to install and run TypeScript locally in npm?

How do I fix the error with Webpack 5?

To fix the error with Webpack 5, update your Next.js config file ( /next.config.js) with the following, it tells webpack not to resolve the module on the client-side ( !isServer ).

How does typescript resolve modules by default?

How TypeScript resolves modules by default Without configuring the TypeScript compiler as discussed earlier, TypeScript will adopt the Node.js run-time resolution strategy by default in order to locate the requested modules at compile-time. To accomplish this, the TypeScript compiler will look for.ts files,.d.ts,.tsx, and package.json files.


2 Answers

Came up with a solution to use index.ts for all exports instead of index.d.ts.

like image 145
zeroin Avatar answered Sep 30 '22 18:09

zeroin


For me, I ended up changing it from './types' to './types.d'.

So a type import would look something like,

import {
    TSomeType,
    ISomeInterface,
    ESomeEnum
} from 'src/types/components/SomeComponent/types.d'

instead of,

import {
    TSomeType,
    ISomeInterface,
    ESomeEnum
} from 'src/types/components/SomeComponent/types'

Taken from this comment:

I can confirm this issue, @Draccoz @filipesilva .

It does not happen when you change the filename from .d.ts to .ts. It also does not happen when you change your import from import { Something } from 'yourlib'; to import { Something } from 'yourlib.d';

I just moved a working project from a webpack-starter seed (TypeScript/Angular) to CLI and got this error, so I assume it has something to do with the generated CLI project.

I'm using @angular/cli: 1.0.0-rc.2


I think that it's also worth to note that tsx files work just fine. I had to change types to types.d only for ts files.

like image 34
Mike K Avatar answered Sep 30 '22 20:09

Mike K