简体   繁体   中英

Typescript baseUrl not working in React Typescript project

I defined the baseUrl in my tsconfig.json file:

"baseUrl": "src",

In .eslintrc.js I have:

parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
  },

Now in eg index.tsx I can import my components like import Layout from 'components/layout';

When I run gatsby develop I get some errors like:

If you're trying to use a package make sure that 'components/layout' is installed. If you're trying to use a local file make sure that the path is correct.
error undefined failed

What's missing here, why it isn't finding my components?

You have to add import/resolver settings in your .eslintrc.js so Webpack can identify imports of your components (I'm supposing that they have .tsx extension)

settings: {
    'import/resolver': {
      node: {
        paths: [
          'src'
        ],
        extensions: [
          '.ts',
          '.tsx'
        ]
      }
    }
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM