How to avoid using relative path imports (/../../../redux/action/action1) in create-react-app
Solution 1
Create a file called .env
in the project root and write there:
NODE_PATH=src
Then restart the development server. You should be able to import anything inside src
without relative paths.
Note I would not recommend calling your folder src/redux
because now it is confusing whether redux
import refers to your app or the library. Instead you can call your folder src/app
and import things from app/...
.
We intentionally don't support custom syntax like @redux
because it's not compatible with Node resolution algorithm.
Solution 2
The approach in the accepted answer has now been superseded. Create React App now has a different way to set absolute paths as documented here.
To summarise, you can configure your application to support importing modules using absolute paths by doing the following:
Create/Edit your jsconfig.json/tsconfig.json in the root of your project with the following:
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
Once you have done this you can then import by specifying subdirectories of "src" (in the following example, components is a subdirectory of src) e.g.
import Button from 'components/Button'
Solution 3
After you try Ben Smith's solution above if you find eslint complains about importing absolute path add the following line to your eslint config:
settings: {
'import/resolver': {
node: {
paths: ['src'],
},
},
},
replace 'src' with your folder if you use your own boilerplate with your folder's name.
Solution 4
We can use webpack 2 resolve property in the webpack config.
Sample webpack config using resolve :
Here component and utils are independent folder containing React components.
resolve: {
modules: ['src/scripts', 'node_modules'],
extensions: ['.jsx', '.js'],
unsafeCache: true,
alias: {
components: path.resolve(__dirname, 'src', 'scripts', 'components'),
utils: path.resolve(__dirname, 'src', 'scripts', 'utils'),
}
}
After that we can import directly in files :
import UiUtils from 'utils/UiUtils';
import TabContent from 'components/TabContent';
Solution 5
Feb 2010
Wasted about an hour on this.
An example is below:
Goal: Import App.css
in HomePage.js
myapp\src\App.css
myapp\src\pages\HomePage.js
File: jsconfig.json
{
"compilerOptions": {
"baseUrl": "src"
}
}
File: src\pages\HomePage.js
import "App.css";
Prem
I'm a passionate developer mainly focusing on web applications and have a hands on experience on frontend with reactjs, angularjs etc. On Backend i do have experience in Nodejs, PHP, Python and usually used to deploy them on AWS or GCP. Although an AWS fanboy i do work with GCP sometimes. I do make useful youtube videos on web development don't forget to visit my channel https://youtube.com/dcoders
Updated on January 05, 2022Comments
-
Prem over 2 years
I've been using create-react-app package for creating a react website. I was using relative paths throughout my app for importing components, resources, redux etc. eg,
import action from '../../../redux/action
I have tried using module-alis npm package but with no success. Is there any plugin that I can use to import based on the folder name or alias i.e. an absolute path?
Eg.,
import action from '@redux/action'
orimport action from '@resource/css/style.css'
-
Prem almost 7 yearsI use create-react-app package where i cannot modify the webpack config. Is there any other way of doing it?
-
santosh almost 7 yearswebpack.config.js is available in below path :
/create-react-app/blob/master/packages/react-scripts/config/webpack.config.dev.js
/create-react-app/blob/master/packages/react-scripts/config/webpack.config.prod.js
-
Dan Abramov almost 7 yearsThis won't work with Create React App. It does not support custom Babel plugins.
-
Dan Abramov almost 7 yearsNo, it is not supported to modify these configs. You can eject but then you lose many benefits of CRA. Please see my answer for a supported way of doing this.
-
yerassyl over 6 yearsCan I do this in react-create-app? I tried, but it seem not to work, although guy from here medium.com/@ktruong008/… suggest the same
-
qwang07 over 6 years@Mehari what's your version of create-react-app and react-scripts?
-
mehari over 6 years@qwang create-react-app-1.4.3 & react-scripts-1.0.14
-
Prem about 6 yearsMore details about implementation can be found here - youtu.be/xHiCRpfZIO8
-
jwanglof almost 6 yearsThis works for us locally but not when we're building in Jenkins (we have a React-app within a Node-app), anyway, we got around this problem to add this to React's package.json instead: {"scripts": {"build": "NODE_PATH=./src react-scripts build"}} Now the build works in our Jenkins setup as well. Thanks for the tip!
-
jwanglof almost 6 yearsThis gave me a lead how to fix our "react-scripts build". See my comment in @dan-abramov 's answer (stackoverflow.com/a/45214138/2986873). Thanks!
-
Agustin Meriles about 5 yearsCurrently, create-react-app is showing a deprecated warning if you use this approach. The recommended way is now to use a jsConfig.json file:
{ "compilerOptions": { "baseUrl": "src" } }
-
Chasen Bettinger almost 5 yearsMore information about Agustin's comment found here: facebook.github.io/create-react-app/docs/…
-
Con Antonakos over 4 yearsTwo questions: (1) I'm having a hard time importing a folder with index.js. It won't implicitly understand to point to index.js to find the exports. (2) Can we customize the alias of the absolute import?
-
Zach Smith over 4 yearsthink of the unfortunate new developer who starts working on your code base and has to deal with the ambiguity of npm packages vs folder names!!
-
Ben Smith over 3 yearsCheers for the reference and +1 for getting eslint to work nicely with the solution.
-
Vigneshwaran Chandrasekaran over 3 years
-
Ben Smith over 3 years@VigneshwaranChandrasekaran that link is already included in my answer. See "...way to set absolute paths as documented here."
-
dmitri about 3 yearsthat option is depricated and should not be used...
-
puerile over 2 yearsI like this solution better. 👍