next-transpile-modules: baseUrl doesn't work within transpiled repo

  • I HAVE READ THE FAQ AND MY PROBLEM WAS NOT DESCRIBED THERE
  • I WILL GIVE 10$ TO CHARITY IF MY SOLUTION WAS ACTUALLY IN THE README/FAQ

Are you trying to transpile a local package or an npm package? npm package (a github dependency)

Describe the bug Using baseUrl to have absolute imports doesn’t work within the transpiled repo.

To Reproduce

  1. Repo A is a Next.js Repo
  2. Repo B is a Next.js Repo
  3. Repo A & Repo B use baseUrl to have absolute imports.
  4. In Repo A, import a component in Repo B that uses an absolute import and you receive an error.

Expected behavior It should not break. 😇

Setup

  • Next.js version: 10.0.4
  • next-transpile-modules version: 6.0.0
  • Node.js version: 12.20.0
  • npm/yarn version: 6.14.8
  • Operating System: OS X 10.15.6
  • Webpack 4 or 5: 4.44.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

bump! this is necessary especially now that its being used with Vercel’s turborepo - will try to contribute here!

EDIT: nextjs app needs to reference the aliased paths in packages for compilation to work. I must have had a typo.

@martpie I found a solution with tsconfig-paths-webpack-plugin.

// next.config.js

const { join } = require('path');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

const workspace = join(__dirname, '..');

module.exports = {
  webpack: (config, options) => {
    config.resolve.plugins = [
      ...config.resolve.plugins,
      new TsconfigPathsPlugin({ configFile: '../local-package/tsconfig.json' }),
    ];
    config.module.rules = [
      ...config.module.rules,
      {
        test: /\.(js|jsx|ts|tsx)$/,
        include: [workspace],
        exclude: /node_modules/,
        use: options.defaultLoaders.babel,
      },
    ];
    return config;
  },
};

Nevertheless, in the end I decided to change the absolute paths to relative ones, because more problems followed. Thanks for your quick answers.

Never tried this feature, I’ll have to have a look 😃

@amlcodes can you open a new issue and fill the issue template there? This issue is quite old.

I think it’s worth adding into the README that ntm does not have baseUrl support. When deciding to use this repo, I looked at its restrictions and support to make sure I can use it so it would be good to rule it out up front.