Next.js: TSConfig auto-generated generated in the wrong location

Created on 8 Jul 2019  路  10Comments  路  Source: vercel/next.js

Bug report

Describe the bug

When using TypeScript and a custom project directory, tsconfig.json is auto-generated in the wrong location. This file should be located in the root of your project.

To Reproduce

  1. create a new project with NextJS 9.0.0
  2. create src/ts/pages and add index.tsx
  3. run next build src/ts (note the custom project directory)

NextJS will see that you are building a TypeScript project and that you do not have a tsconfig.json and will generate one for you. Note that this file is generated in src/ts instead of where you ran next from (the project root).

Expected behavior

tsconfig.json is generated in the project root.

System information

  • OS: MacOS 10.14 (but I assume all OSes)
  • Version of Next.js: 9.0.0
needs investigation

Most helpful comment

Yeah, came by to say the same thing. Having a special tsconfig.json that just extends the real one because the autogenerated file is not put at the root of the project, as it should be, is... not great.

I also don't think it's yet been mentioned that next-env.d.ts is also generated in the same place, even though I have a types directory for custom type files.

@Timer monorepos are growing ever more popular. but even if they weren't, consistency of app layout between repositories is important. it's not unusual for repos to have put their source in '/src', types in some other place, and keep configuration elsewhere. babelrc, eslint, nodemon configs etc all live at project roots. tsconfig.json is supposed to too.

Please let us specify through something in next.config.js, the locations of both tsconfig.json and next-env.d.ts. The magic autogeneration is a reasonable fallback for those who don't care.

All 10 comments

Hey @tills13!

This is the expected behavior because Next.js might require specific TypeScript settings that do not agree with the rest of your application.

If you feel strongly about relocating the tsconfig.json file, you can move it to your <rootDir> and then create a stub tsconfig.json in the src/ts/ folder with the contents:

{ "extends": "../../tsconfig.json" }

That solution works for me - thanks!

@timer That's an ugly workaround (imho), I only keep actual application code in ./src. Guess I'll stick with Next.js V8 for the time being then. Edit; related: https://github.com/zeit/next.js/pull/7798#issuecomment-509666217, https://spectrum.chat/next-js/general/nextjs-9-typescript-and-tsconfig~a3354a2d-63be-4573-ab41-5bc05a647bd0.

@wouterds this feature is typically only used for monorepos (yes, ./src is a form of monorepo) where different tsconfig.jsons are necessary.

While I'm ok with the above solution, it seems like tsconfig.json in particular should live where your next.config.js lives, no?

For reference if we could go back in time next.config.js would not resolve up the tree and have the requirement to be inside the project directory.

Yeah, came by to say the same thing. Having a special tsconfig.json that just extends the real one because the autogenerated file is not put at the root of the project, as it should be, is... not great.

I also don't think it's yet been mentioned that next-env.d.ts is also generated in the same place, even though I have a types directory for custom type files.

@Timer monorepos are growing ever more popular. but even if they weren't, consistency of app layout between repositories is important. it's not unusual for repos to have put their source in '/src', types in some other place, and keep configuration elsewhere. babelrc, eslint, nodemon configs etc all live at project roots. tsconfig.json is supposed to too.

Please let us specify through something in next.config.js, the locations of both tsconfig.json and next-env.d.ts. The magic autogeneration is a reasonable fallback for those who don't care.

Not to mention that even if we use the workaround, the builder still automatically adds more fields to the config, including exclude: ["node_modules"] pointing to the wrong relative path.

Combined with the issue of failed imports from named exports (https://github.com/zeit/next.js/issues/7882), I can't move to Next 9, which is a real shame.

I only notice now, but this has been fixed 9.0.7. I can finally use the src folder out of the box! https://github.com/zeit/next.js/issues/8451

I actually think this is a good feature,

I have a project which the root represents the server code

  • all the server code resides in ./server
  • nextjs sits in ./client
  • nextjs builds into './public' and server code builds into './lib'
  • express server './public' for public assets

I was worry about Next.js will pick up the tsconfig.json from the root which gonna break everything as it says so in the doc. But it ends up creating a new tsconfig.json inside ./client folder, fantastic!!!

image

Deployment is so easy for this, otherwise you need to move around the client building bundle to the folder that server code used to serve.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

havefive picture havefive  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

rauchg picture rauchg  路  3Comments

knipferrc picture knipferrc  路  3Comments