How to tell Visual Studio code about relative path

I am creating a responsive application using the create-react-app tool, so it uses webpack to bundle modules. Having the following directory structure

-my-app
--node_modules
--src
---components
----somecomponent
-----SomeComponent.jsx
----someothercomponent
-----SomeOtherComponent.jsx
----main.js
----public

To avoid many ../../(dotted slashes for a relative path), I set NODE_PATH=./srcas shown below in my package. json

"scripts": {
    "start": "NODE_PATH=./src react-scripts start",
    "build": "NODE_PATH=./src react-scripts build",
}

So now I can import my modules in my style

import SomeComponent from "/components/somecomponent/SomeComponent"

Thus, even changing the directory structure will not break my code so often. But with this, my VSCode does not recognize the paths and thus does not show intellisense, how can I fix it?

+4
source share
1 answer

JS TS VSCode. , , :

jsconfig.json baseUrl:

{
    "compilerOptions": {
        "baseUrl": "./src"
    }
}

, :

import SomeComponent from "components/somecomponent/SomeComponent"

paths:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "~/*": ["./src/*"]
        }
    }
}

:

import SomeComponent from "~/components/somecomponent/SomeComponent"

+7

Source: https://habr.com/ru/post/1672550/


All Articles