0

I’m migrating a codebase from Flow to TypeScript. I’m getting Cannot find module 'SOME DEPENDENCY' or its corresponding type declarations.ts(2307) for various dependencies which include typings in the dependency itself.

Example of one such dependency: ./node_modules/axios/index.d.ts

My project (among many other things) has:

./tsconfig.json
./src/SomeFileUsingAxios.tsx
./node_modules
./vendor/node_modules
./vendor/node_modules/axios

I’ve tried "typeRoots": ["./node_modules/@types", "./vendor/node_modules/@types"] I’ve tried "rootDirs": ["./", "vendor/"] I’ve tried both together ^

Can’t seem to make TypeScript aware of this dependency! Any ideas?

1 Answer 1

1

In the "node" module resolution strategy the compiler looks for modules in node_modules for the current folder and then up the tree. In the project structure that you provided, it would look for axios in ./src/node_modules which does not exist, then ./node_modules which does not have axios, but it would not look in ./vendor/node_modules.

In order to fix the problem, it could work to install the axios module in the main package, or to provide a path mapping for typescript to find the module. Something like this:

paths: {
  '*': './vendor/node_modules/*',
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I had tried ` paths: { '': './vendor/node_modules/', } ` At some other earlier point to no avail; however, I see how you only need to be explicitly about the extra paths not dealt with by the module resolution behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.