1

I have just created a new project and I am trying to setup an absolute path by following this post: https://medium.com/geekculture/making-life-easier-with-absolute-imports-react-in-javascript-and-typescript-bbdab8a8a3a1

I have followed the steps as close as I can but can not get this to work in my simulator. (Visually there are no errors when looking at my code in IDE)

Terminal error

eslint-config.json

{
  "eslintConfig": {
    "extends": ["react-app", "airbnb"],
    "settings": {
      "import/resolver": {
        "node": {
          "paths": ["src"],
          "extensions": [".js", ".jsx", ".ts", ".tsx"]
        }
      }
    }
  }
}

tsconfig.json

{
  "extends": "@tsconfig/react-native/tsconfig.json",     /* Recommended React Native TSConfig base */
  "compilerOptions": {
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */
    "baseUrl": "src",
  },
}

In this image you can see my file structure, the error on my simulator. And how I am importing it.

enter image description here

I have two exported functions in my Text.tsx export function Title & export function P

Any help is appreciated... Thank you!

2
  • can you please send us the folder structure of your project and where is the views folder? Commented Jul 9, 2022 at 18:22
  • Hi, @NishargShah I have included the file structure in the last image. If this is not sufficient, let me know! Commented Jul 9, 2022 at 18:23

3 Answers 3

2

In my case (react-native: 0.73.1) I was able to resolve it by enabling global package option in

//metro.config.js

const config = {
  resolver: {
    enableGlobalPackages: true,
    // ...
  },
  // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

This saved me a whole lot of time
didn't work for me unfortunately, this should be the alternative to yarn workspaces right ?
1

I will not suggest you to go with this flow because it's very confusing.

Suppose you had imported this react/client thing.

  • Is it the npm package that contains subpath
  • Is it my folder structure pathname

So I suggest you to go with the flow that most of the developers are using currently.

import Text from "@/views/Text";

I made one demo for you also, please check https://codesandbox.io/s/sleepy-dew-wn2rsu

Note

  • Use eslint-plugin-import for removing import errors.

2 Comments

Hey! I am not doubting that this is the correct approach. But I struggling to understand what I need to add from that codesandbox. I have installed eslint-plugin-import and @typescript-eslint/parser. I have added baseUrl and paths from the sandbox. I have added the import Text from "@/views/Text" - but "...text/text could not be found within the project or in these directories: node modules". Also it suggests in the docs to use .eslintrc.js and seems different to your approach. So I am just unsure, could you please clarify? Thanks, Jack
If you are using eslint in your project then you need to set up that plugin because without that eslint will not identify that. please check eslint.org/docs/latest/user-guide/configuring/… for your .eslintrc.js question
0

create new package.json file under "src" folder and add { "name": "PATH_NAME" }

Comments

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.