LoginSignup
46
24

More than 3 years have passed since last update.

Next.js+TypeScriptでパスのaliasを設定する

Last updated at Posted at 2020-02-07

Next.js + TypeScript のプロジェクトで相対パス辛いなとなったので、そのエイリアスの設定方法
import { SomeComponent } from '@/components/SomeComponent みたいなやつです

Next.js での path alias

with absolute imports の example があります
https://github.com/zeit/next.js/tree/master/examples/with-absolute-imports

next.config.js
const path = require('path')

module.exports = {
  webpack(config, options) {
    config.resolve.alias['@'] = path.join(__dirname, 'src')
    return config
  },
}

TypeScript での path alias

tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}
46
24
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
24