Johny Whyte
Johny Whyte

Reputation: 11

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

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

Upvotes: 0

Views: 793

Answers (3)

Narendra Chouhan
Narendra Chouhan

Reputation: 2319

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

Upvotes: 0

Mehdi Dehghani
Mehdi Dehghani

Reputation: 11601

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'] };

Upvotes: 1

Joe Lloyd
Joe Lloyd

Reputation: 22323

I think you can use postinstall

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

Upvotes: 0

Related Questions