question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js compiles .ts files outside of the application directory, but not .tsx files

See original GitHub issue

Bug report

Describe the bug

Next.js supports TypeScript, which means that it is able to load .ts and .tsx files from within the application directory. In addition, Next.js also supports loading files from outside the application directory, e.g. to be able to use shared components. However, this only works for .ts files, not for .tsx files. This is unexpected and makes it hard to work with re-usable UI components, without having to come up with a separate npm module for them, which is often not what you need.

To Reproduce

We have set up a sample repository with a small, but complete example to reproduce the issue. Please see https://github.com/thenativeweb/temp--next-issue for details. The README.md file contains the detailed description on how to reproduce the issue.

Expected behavior

Next.js should not only support .ts files from outside the application directory, but .tsx files as well.

System information

  • OS: macOS Big Sur (11.0.1)
  • Version of Next.js: 10.0.3
  • Version of Node.js: 14.14.0
  • Deployment: npx next

Additional context

Setting up a separate npm module for the shared components is definitely not what we want to use. Even the various Next.js applications we have shall be in a single npm module, as they get started altogether using a custom server. So in the end, although our “real” repository contains multiple Next.js applications, there is only one actual application in the end. This is also the reason why we don’t want to go down the next-transpile-modules road (it would force us to introduce separate modules as well).

Given that Next.js is able without any ado to handle .ts files from outside the application directory, we assume that the same should be possible for .tsx files.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
Timercommented, Dec 8, 2020

We shouldn’t support importing .ts files, so we probably need to fix this behavior to be in line with .tsx.

2reactions
Timercommented, Dec 9, 2020

I’ll provide additional background:

Next.js currently only compiles assets that it considers to be first-party to your application. i.e. packages within the project root where next build is invoked from.

This was an intentional design decision and is in-line with other tools like Create React App (FWIW CRA has an even stricter restriction of forbidding even .js outside of root).

The thought process behind this decision is that code outside of your project’s directory may be on different version(s) of packages or using different syntax features not yet supported by Next.js, e.g. React 16 vs 17 (using the new JSX transform) or TypeScript 3 vs 4 (using new syntax features).

The code for projects outside of Next.js’ project root, even in the same monorepo, should be precompiled with their own tooling (e.g. tsdx) so that Next.js can consume it as it would something from node_modules.

This eliminates the need for you to upgrade all your Next/React/TypeScript/etc versions in tandem across a potentially large monorepo, i.e. it provides isolation by ensuring assets are compiled down to a common interop language: ESLatest JS (same as node_modules).


With the above background stated, I’m treating this issue as a bug report because the current design intent is not to allow .ts imports (or .tsx) outside of the project root.

This doesn’t mean we’re not considering allowing this, and we have an issue open to track allowing this behavior: https://github.com/vercel/next.js/issues/9474

We realize that small monorepos / certain organizations are ok with having to update all their TypeScript/etc versions in tandem, so we’ll let people opt-into this behavior.

You can track this issue (#9474) which would be the feature request version of this issue: to allow importing .ts and .tsx outside of project root.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I fix compilation issues with my TypeScript / Next.js ...
Whenever I rename a file from Foo.js to Foo.tsx I get a "Failed to compile" error caused by "No such file or directory"....
Read more >
Basic Features: TypeScript - Next.js
A file named next-env.d.ts will be created at the root of your project. This file ensures Next.js types are picked up by the...
Read more >
TSConfig Reference - Docs on every TSConfig option
A TSConfig file in a directory indicates that the directory is the root of a TypeScript or JavaScript project... Compiler Options. Top Level....
Read more >
Common TypeScript module problems and how to solve them
ts . This time, the file does not exist, so the compiler will substitute 'nav/file3' with the second location " ...
Read more >
TypeScript - webpack
tsx files through the ts-loader , and output a bundle.js file in our current directory. Now lets change the import of lodash in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found