Discussion React.js

Absolute Imports in Create React App

By default ES6 modules in create-react-app use relative paths.
But using relative paths is a real pain when you start dealing with deeply nested tree structures because you end up with dot-dot syndrome.

After digging through a bunch of github issues I was finally able to boil down the steps required to implement absolute imports in create-react-app applications down to two steps:

  1. Create a ‘.env’ file at the root level (same level as package.json)
  2. Set an environment variable, ‘NODE_PATH’ to ‘src/’

And that’s it.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *