Setting Up Absolute Imports for Your React, React Native, Typescript and JavaScript Projects

Taye Odunfa
3 min readJun 8, 2024

--

Introduction

As your JavaScript projects grow, managing imports can become increasingly cumbersome. Relative paths like ../../../components/Button not only clutter your code but also make refactoring more difficult. Absolute imports simplify this by allowing you to import files from a project root directory. In this article, we'll walk you through setting up absolute imports for React, React Native, and other JavaScript projects.

Benefits of Absolute Imports

  1. Improved Readability: Cleaner and more readable import statements.
  2. Easier Refactoring: Simplifies moving files around without breaking imports.
  3. Consistency: Consistent import paths across the project.
  4. Differentiation from Package Imports: Using aliases like @app helps clearly distinguish between internal project imports and external package imports.

Setting Up Absolute Imports in React

For React projects, we’ll use Create React App (CRA) as the basis. Here’s how to set up absolute imports:

  1. Modify jsconfig.json
    Create a jsconfig.json file at the root of your project if it doesn't already exist, and add the following configuration:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@app/*": ["*"]
}
},
"include": ["src"]
}

This tells the compiler that the base directory for module resolution is src and sets up the @app alias.

2. Update Import Statements
Change your import statements from relative to absolute paths. For example, change:

import Button from '../../../components/Button';

to:

import Button from '@app/components/Button';

Setting Up Absolute Imports in React Native

For React Native projects, the setup is slightly different due to the lack of a built-in support similar to CRA. Here’s how you can configure it:

  1. Install Babel Plugin
    First, install the necessary Babel plugin:
npm install --save-dev babel-plugin-module-resolver

2. Configure Babel
Modify your Babel configuration (usually found in .babelrc or babel.config.js):

{
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"@app": "./src",
}
}
]
]
}

3. Update Import Statements
Update your import statements in your React Native project similar to how we did for React:

import Button from '@app/components/Button';

Setting Up Absolute Imports in TypeScript Projects

For TypeScript projects, you need to configure tsconfig.json and optionally use Babel for additional support. Here's how you can do it:

  1. Modify tsconfig.json
    Add the following configuration to your tsconfig.json file:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@app/*": ["*"],
}
}
}

This configuration sets the base URL for your project and maps paths for your modules using the @app alias.

2. Configure Babel (Optional)
If you’re using Babel in your TypeScript project, you can configure it as follows:

{
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"@app": "./src",
}
}
]
]
}

3. Update Import Statements
Change your import statements to use absolute paths:

import Button from '@app/components/Button';

Setting Up Absolute Imports in Other JavaScript Projects

For other JavaScript projects, you can use a combination of Babel and Webpack. Here’s a generic setup:

  1. Install Babel Plugin
npm install --save-dev babel-plugin-module-resolver

2. Configure Babel
Add the following to your .babelrc or babel.config.js:

{
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"@app": "./src",
}
}
]
]
}

3. Configure Webpack
Modify your webpack.config.js to support absolute imports:

const path = require('path');

module.exports = {
resolve: {
alias: {
"@app": path.resolve(__dirname, 'src'),
}
}
};

4. Update Import Statements
As with the previous setups, change your import statements to use absolute paths:

import Button from '@app/components/Button';

Conclusion

Setting up absolute imports in your React, React Native, and other JavaScript projects can significantly improve your development experience. By simplifying your import statements, you enhance the readability and maintainability of your code. Implementing this in your project is straightforward and can save you a lot of time in the long run.

Happy coding!

--

--