Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readReactDev Tool

Absolute imports in React projects

Use imports relative to src in Create React App or configure an @ alias in Next.js.

Relative imports get harder to read as a React project grows. A path like ../../components/button also changes if you move the file that imports it.

A relative import climbs the tree with ../. With baseUrl set to src, the import starts at src and stays the same if the importing file moves.

Create React App

Add this to jsconfig.json at the project root, or merge the compiler option into tsconfig.json for TypeScript.

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

In Create React App, you can now import src/components/button.js as import Button from 'components/button'.

February 2025 update. React deprecated Create React App for new apps. This setup still applies to existing CRA projects.

Next.js

Next.js supports path aliases in the same config files. If your code lives under src/, merge in these options.

{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

Now the import reads import Button from '@/components/button'. March 2026 update. TypeScript 6 deprecated baseUrl. The Next.js example uses paths relative to the config file, which works without baseUrl.

Other React setups depend on the bundler. TypeScript uses paths to resolve imports during checking and leaves the import strings as written in its output.