DEV Community

Discussion on: How Pros Get Rid of Relative Imports

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

I think it works with React as well. Tried it in a Next.js project though

Collapse
 
talorlanczyk profile image
TalOrlanczyk

For react you need third party library

Thread Thread
 
ruppysuppy profile image
Tapajyoti Bose

I tested it right now, it's working with plain old react

Thread Thread
 
talorlanczyk profile image
TalOrlanczyk • Edited

weird I copy this exactly and the paths didn't work
the error:Module not found: Can't resolve '@models/tests' in '/Users/talorlanczyk/Projects/react-tests/my-app/src'
I would like you to show me the code on react please

Thread Thread
 
ruppysuppy profile image
Tapajyoti Bose

Add the following in webpack.config.js:

module.exports = {
    // ...
    resolve: {
        alias: {
            // path to your models
            '@models': path.resolve(__dirname, './src/models'),
        },
    }
}
Enter fullscreen mode Exit fullscreen mode

Use yarn eject first if you are using create-react-app

Thread Thread
 
talorlanczyk profile image
TalOrlanczyk

Thanks,
But when i say you can't meant that you must do an additional step like eject to do this or use third party library
I think add this to your article will great for a lot of developers