Import project assets using absolute paths from the project root.
Typically somewhere deep in your project you end up with something like this:
const dateUtil = require('../../../utils/date')
With absolute-imports
, you now do this:
const dateUtil = require('your-app-name/utils/date')
Works with React Native and Node.
yarn add absolute-imports
absolute-imports
only needs to be run once. However, if you happen to clean your node_modules
, it needs to run again.
For one-time execution, run the following command:
node absolute-imports --name=your-app-name
To run the command each time you clean your node_modules
, add it as a postinstall
script:
// package.json
{
...
"scripts" {
...
"postinstall": "node absolute-imports --name=your-app-name"
},
}
This package isn't even needed for Webpack. Add the following to your Webpack config:
resolve: {
modules: [process.cwd(), 'node_modules'],
alias: {
'your-app-name': process.cwd(),
},
},