Advanced nextraThe next.js @ alias

The meaning of @in next.js

In Next.js, @ is not a built-in alias by default; however, it is commonly set up by developers to make imports easier to read and manage, often as an alias for the root of the project or specific directories.

To define @ as an alias in your Next.js project, you can add it to the jsconfig.json or tsconfig.json file in the root of your project. Here’s how to set it up:

  1. Open or create jsconfig.json (or tsconfig.json for TypeScript projects).

  2. Add the following configuration:

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

This configuration tells Next.js to interpret @ as the root directory, allowing you to import files from the root without using relative paths. For instance, with @ defined this way, you can write:

import Component from "@/components/Component";

instead of using ../../../ to reach a file.

After defining this alias, you don’t need additional configuration, as Next.js will automatically respect the paths defined in your jsconfig.json or tsconfig.json.