Next.js + typescriptでpathsのエイリアスがModule not foundになる

公開日時

以前、webpack + typescriptでpathsのエイリアスがModule not foundになるというのを書いたが、Next.js + typescriptでも同様の現象が起きたので対応。

yarn add -D tsconfig-paths-webpack-plugin

webpack.config.tsの代わりに[[next.config.js]]に以下を記述すればエイリアスが使えるようになった。

const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");

module.exports = {
  webpack: (config, options) => {
    if (config.resolve.plugins) {
      config.resolve.plugins.push(new TsconfigPathsPlugin());
    } else {
      config.resolve.plugins = [new TsconfigPathsPlugin()];
    }

    return config;
  }
};

参考


Related #js