Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support baseUrl in tsconfig #7935

Closed
chiller-whale opened this issue Jul 12, 2019 · 12 comments
Closed

Support baseUrl in tsconfig #7935

chiller-whale opened this issue Jul 12, 2019 · 12 comments

Comments

@chiller-whale
Copy link

chiller-whale commented Jul 12, 2019

When using the baseUrl property intsconfig.json, Next 9 fails to build with the following error:
Module not found: Can't resolve ...

This is resolved by updating the import path to be absolute.

When running tsc on the same directory, the relative import works.

Expected behavior

The baseUrl in tsconfig.json is used when running next build

To Reproduce

Update tsconfig to use baseUrl compiler option:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": "."
  },
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}

Assuming a directory structure of:

components
  |--> Layout.tsx
pages
  |--> index.tsx

Import file using relative path:

  // pages/index.js
  import Layout from 'components/layout'

  export default (props) => <Layout>Welcome!</Layout>

package.json

"dependencies": {
    "next": "^9.0.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
  },
"devDependencies": {
   "typescript": "^3.5.3"
}
@PabloSzx
Copy link
Contributor

PabloSzx commented Jul 13, 2019

I needed this feature so i did a workaround using tsconfig-paths-webpack-plugin

My next.config.js ended up like this

Code_-_Insiders_NJxLaKK5R0

EDIT: I just made a template repository using it https://github.com/PabloSzx/Next-TypeScript-Paths

@Timer
Copy link
Member

Timer commented Jul 14, 2019

Duplicate of #7779

@Timer Timer marked this as a duplicate of #7779 Jul 14, 2019
@Timer Timer closed this as completed Jul 14, 2019
@EricVanDerDijs
Copy link

@PabloSzx Thanks a lot for that comment, I have been looking for solutions for this for a long while now.

@sakulstra
Copy link
Contributor

@PabloSzx does this actually work for you?

I tried adding as next.config.js

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

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

    return config;
  },
  target: "serverless"
};

and

    "baseUrl": "."

inside the tsconfig, but root imports still don't seem to be working (may be due to me mixing js and tsx files).

@PabloSzx
Copy link
Contributor

@sakulstra i just made a Next 9 template repository which uses TypeScript and tsconfig-paths-webpack-plugin https://github.com/PabloSzx/Next-TypeScript-Paths

@HosseinAgha
Copy link

@PabloSzx you need to use .push here instead of calling plugins as a function

@zhengrenzhe
Copy link

@PabloSzx does this actually work for you?

I tried adding as next.config.js

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

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

    return config;
  },
  target: "serverless"
};

and

    "baseUrl": "."

inside the tsconfig, but root imports still don't seem to be working (may be due to me mixing js and tsx files).

config.resolve.plugins is an array, should be this:

if (config.resolve.plugins) {
      config.resolve.plugins.push(new TsconfigPathsPlugin());
} else {
      config.resolve.plugins = [new TsconfigPathsPlugin()];
}

:D

@blainegarrett
Copy link

@zhengrenzhe @PabloSzx Out of curiosity, do you have the tsconfig-paths-webpack-plugin installed as a devDependecy or normal dependency. I was surprised to find that running the prod build errored on the import in next.config. For some reason, I thought next.config would be moot in a production build. I normally only use it for the runtimeConfig. I'm wondering if there is a performance hit with it running in the built production app... or maybe at that point its just a useless import?

@PabloSzx
Copy link
Contributor

PabloSzx commented Apr 7, 2020

@blainegarrett

It doesn't matter in the final build, and the path equivalents are made in compilation time.

Although, there is an experimental official support for this feature
Check out this #11293

@timneutkens
Copy link
Member

You'll probably want to use the experimental support from #11293 indeed. It'll be on stable very soon.

@regalstreak
Copy link

For me, restarting the Next.js dev server worked haha

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests