Carven
Carven

Reputation: 15660

How to configure webpack and Typescript to exclude resolving a specific typing-only package?

I'm using only the types package of @types/aws-lambda for type declaration within my code. I'm not using its actual package, just its typings.

In my code, I would still import the typings as though it is a normal module though, something like this:

import { APIGatewayEvent } from 'aws-lambda';

const myApi: APIGatewayEvent = someEvent;

However, when I try to compile through webpack, I get an error:

Module not found: Error: Can't resolve 'aws-lambda' in....

Is this an issue with webpack or Typescript trying to resolve the module? How can I configure webpack and in tsconfig to exclude attempting to resolve aws-lambda which in my case is only the typings to resolve the error?

Upvotes: 1

Views: 1383

Answers (2)

Dumidu Pramith
Dumidu Pramith

Reputation: 414

in typescript, first, you need to tell tsc compiler, this is a new module. for that, You need to add declare keyword. and also, in this case, you're using aws-lambda as an interface for real-time IntelliSense. for that you need to add typeof keyword. like that

import { APIGatewayEvent } from 'aws-lambda';

declare const myApi: typeof APIGatewayEvent

Upvotes: 0

automasean
automasean

Reputation: 421

Try configuring webpack to resolve .ts extensions.

module.exports = {
  //...
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  }
};

Also make sure you have the proper TypeScript integration configuration.

You can also double-check your tsconfig.json file. If you're explicitly setting typeRoots then you'll want to include "./node_modules/@types".

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

If typeRoots is specified, only packages under typeRoots will be included

Upvotes: 0

Related Questions