Toby Maguire
Toby Maguire

Reputation: 154

getting rid of relative path react (removing "../..")

I have downloaded a react project with the following package.json :

{
  "private": true,
  "main": "src/index.js",
  "dependencies": {
    "@material-ui/core": "4.10.0",
    "@material-ui/icons": "4.9.1",
    "classnames": "2.2.6",
    "history": "4.10.1",
    "moment": "2.26.0",
    "node-sass": "4.14.1",
    "nouislider": "14.5.0",
    "prop-types": "15.7.2",
    "react": "16.13.1",
    "react-datetime": "2.16.3",
    "react-dom": "16.13.1",
    "react-router-dom": "5.2.0",
    "react-scripts": "3.4.1",
    "react-slick": "0.26.1",
    "react-swipeable-views": "0.13.9"
  },
  "devDependencies": {
    "@babel/cli": "7.10.1",
    "@babel/plugin-proposal-class-properties": "7.10.1",
    "@babel/preset-env": "7.10.1",
    "@babel/preset-react": "7.10.1",
    "eslint-config-prettier": "6.11.0",
    "eslint-plugin-prettier": "3.1.3",
    "eslint-plugin-react": "7.20.0",
    "gulp": "4.0.2",
    "gulp-append-prepend": "1.0.8",
    "prettier": "2.0.5"
  },
  "optionalDependencies": {
    "typescript": "3.9.3"
  },
  "scripts": {
    "start": "NODE_PATH=src/ react-scripts start",
    "build": "react-scripts build && gulp licenses",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "lint:check": "eslint . --ext=js,jsx;  exit 0",
    "lint:fix": "eslint . --ext=js,jsx --fix;  exit 0",
    "install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start",
    "build-package-css": "node-sass src/assets/scss/material-kit-react.scss dist/material-kit-react.css",
    "build-package": "npm run build-package-css && babel src --out-dir dist",
    "compile-sass": "node-sass src/assets/scss/material-kit-react.scss src/assets/css/material-kit-react.css",
    "minify-sass": "node-sass src/assets/scss/material-kit-react.scss src/assets/css/material-kit-react.min.css --output-style compressed",
    "map-sass": "node-sass src/assets/scss/material-kit-react.scss src/assets/css/material-kit-react.css --source-map true"
  }
  
}

I dont know how the developer got rid of relative path in his code. for example he imports modules like:import Header from "components/Header/Header.js"; although if I try to the same , i should do it like:import header from "../../../src/components/Header/header.js".

the link to the original github: https://github.com/creativetimofficial/material-kit-react/

i have downloaded and installed this one , it works fine. but when i try to copy some of the codes,it doesnt work.

Upvotes: 2

Views: 1920

Answers (2)

Allan Mwesigwa
Allan Mwesigwa

Reputation: 1298

In the root directory of your project, create a jsconfig.json file and save this;

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

If you are using eslint in your project, you'll need to add this in your .eslintrc.js file;

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

You should be able to import files by their absolute path now.

Upvotes: 1

Bourbia Brahim
Bourbia Brahim

Reputation: 14712

use the jsconfig.json to set absolute path instead of reletive , as described in documentation

in the provided project he used

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

so he could accees all files using a absolute path , in all folder and subfolders of the project like

import file from "component/file/file.js"

Upvotes: 5

Related Questions