Setting Up Absolute Paths in create-react-app
Setting up a React project can be a fun exercise until you decide to move things around and noticed that all of local imports are broken. To avoid that, you can use absolute paths for imports.
NODE_PATH
is a special environment variable that instructs Webpack to resolve from this folder. Assuming a project containing two folders in the src
, containers
and components
the difference would look like:
Current Code
New Code
With the new structure, we can safely move our files around without worrying about where we are in the directory stack. To achieve this, we create a .env
file in the root of our directory with NODE_PATH
set to src
Next time we run the start command, it will pick up the path automatically 🎉
No comments yet. Share on Mastodon and see your comment or write a post on your blog if you support Webmentions
No reposts yet. Share on Mastodon and see your repost or write a post on your blog if you support Webmentions
No likes yet. Share on Mastodon and see your like or write a post on your blog if you support Webmentions
No bookmarks yet. Share on Mastodon and see your bookmark or write a post on your blog if you support Webmentions
Powered by Webmentions