11

I've started a react-native project generated using the cli. The app seems to be working just fine but i've notice that the import points to typescript cache instead of local node_modules. I'm not even using typescript.

IDE: vscode 1.19.3

import React, { Component } from 'react';

module "/Users/MyMac/Library/Caches/typescript/2.6/node_modules/@types/react/index"

2
  • 1
    @types is a collection of typescript definition packages and not the actual library itself. So in your case it is only loading the type definitions for React from this directory. I'd guess vscode does this to provide intellisense even when you're not using TypeScript.
    – Tao
    Jan 28, 2018 at 12:04
  • 2
    Only problem is that it breaks my app every time it does.. (react-native)... the relative paths don't work
    – Spock
    Aug 1, 2018 at 10:56

1 Answer 1

3

This is expected for JavaScript. These @types files power VS Code IntelliSense for npm modules such as react. The react module itself will still be imported from your local node_modules

See our documentation on Automatic type acquisition for more details

4
  • 4
    Isn't it possible for this to be misleading? For example, VS Code won't show an import error for a package that isn't installed locally in the module if it just so happens to be installed in the .../Caches/typescript/4.0/node_modules/@types/ directory?
    – Raphael
    Sep 28, 2020 at 19:22
  • 1
    I know it's an old question but... Can't this behavior cause some problems? I just encountered one. My React Native project uses React 17, and because the TS cache references @types/react for React 18, I get type errors everywhere. Didn't find much about how to solve that, either on SO or in VSCode's GH issues.
    – Benito
    Apr 20, 2022 at 23:01
  • 1
    @benito, Check the typescript.disableAutomaticTypeAcquisition config option
    – mmomtchev
    Jul 25, 2022 at 15:37
  • @mmomtchev I'd eventually figured it out, out of bare necessity, but thanks!
    – Benito
    Jul 26, 2022 at 9:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.