Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript can not find locally linked module?

I'm trying to test some typescript definitions that I made for a node project that doesn't have any. They work already when I include them in my project that consumes the target node project, or when I make my own folder in node_modules/@types/ and put them there(node_modules/@types/chrome-aws-lambda/index.d.ts). This is using node 8.10, npm 5.6 and typescript 3.1

However, I have forked the repository for the module from github and am hoping to include it in the project so others can use it. In order for this to happen, I have edited the forked project's package.json to point types to source/index.d.ts that lives beside the main, source/index.js file. Then in my project I link it using npm link ../chrome-aws-lambda.

This does not work. When I run tsc it can not find the module it says. error TS2307: Cannot find module 'chrome-aws-lambda' When I run it with the --traceResolution it doesn't even seem like it tries to look for it.

I've figured that it may have something to do with the package not being really 'installed' so I've tried to just copy the project directory into the node_modules instead of linking it. I've also thought perhaps something wasn't working and typescript wasn't reading the types field in the package.json so I put one in the root as that's the default. I thought perhaps it wasn't being included and so I added it in the include part of my .tsconfig. I've tried to change my baseUrl, paths, typeRoots, and moduleResolution in the config file as well.

None of this works.

I don't want to submit a PR without being able to know if it works or not. Does anyone have any clues as to what to do here? I'll gladly add any relevant information needed, please just let me know! I've included the index.d.ts as well as the import statement below in case I'm just doing something stupid there.

Thanks!

index.d.ts:

declare module 'chrome-aws-lambda' {
    import * as Puppeteer from 'puppeteer';

    export default class Chromium {
        static args : Array<string>;
        static defaultViewport : {
            width : number,
            height : number,
            deviceScaleFactor : number,
            isMobile : boolean,
            hasTouch : boolean,
            isLandscape : boolean
        };
        static executablePath : Promise<string>;
        static headless : boolean;
        static puppeteer : typeof Puppeteer;
    }
}

as well as the import statement in question:

import Chromium from 'chrome-aws-lambda';

like image 742
samuraiseoul Avatar asked Nov 26 '18 21:11

samuraiseoul


People also ask

Can not find TypeScript module?

To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command. Copied!

Can not find module TSC?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

How do I link a local npm module?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

Can not find module VSCode?

Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes. If you're still getting the "Cannot find module 'prettier'" error, open your package. json file and make sure it contains the prettier package in the devDependencies object.


1 Answers

According to this answer, you have to make your .d.ts file a module. Hope it will help.

Also, you don't have to put .d.ts to special path, it can be in project at any place. I think this discussion will be very helpful.

like image 168
Inflight Avatar answered Nov 03 '22 10:11

Inflight