How do i have an assets file that i can use in any file in my react app without always having to navigate to the assets folder

478 Views Asked by At

How to use import "assets/css/" in any directory within my app instead of this import "../assets/css/"

3

There are 3 best solutions below

0
Joe Lloyd On

I think you can use postinstall

It allows you to create aliases for different folders in your app. check it here

3
Mehdi Dehghani On

It called Absolute Path Imports

If you using create-react-app, create file called .env with following content at the root of your project and restart the dev server and you are good to go:

NODE_PATH=src/

Otherwise add following to webpack.config.js (Module Resolution):

resolve: { modules: [path.resolve(__dirname, './src'), 'node_modules'] };
0
Narendra Chouhan On

I think you should keep your file in the public Folder and then you can try this directly without importing.

Reference:

https://stackoverflow.com/a/63961058/8425297