简体   繁体   中英

Use absolute path in React components

How I can use absolute path from root and change root, for import components?

import Modal from project/app/src/Components/Modal
import Main from ../../../../Constants

and I want, change root directory to I can import from src

import Modal from Components/Modal
import Main from Constants

I try change workspaces in package.json

"workspaces": [
    "project/app/src/*"
]

or use

"scripts": {
    "start": "NODE_PATH=project/app/src/ webpack-dev-server"
}

but it's not working.

.env file with NODE_PATH=src is deprecating.

use jsconfig.json in root dir instead

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

在 create-react-app 中实现绝对导入首先你需要在根级别(与 package.json 相同级别)创建一个'.env'文件,然后设置一个环境变量, 'NODE_PATH''src/'

NODE_PATH=src

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