next.js: Cannot import Typescript files from outside of 'dir' directory
Bug report
When using a custom server, trying to import a .ts file from outside of the dir directory results in the following error:
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
To Reproduce
- Create a custom server and set the ‘dir’ to something other than the root.
// server.ts
const app = next({
dev: true,
dir: path.resolve(__dirname, './src')
});
-
Create a different
.tsfile outside of./src -
Create a page and try to import the .ts file outside of
./src
// src/pages/index.tsx
import config from '../../config'
Expected behavior
The file is imported correctly with no errors
System information
- OS: macOS
- Next.js 9.1.4
Additional context
I am using ts-node to execute the server file.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 20
- Comments: 23 (9 by maintainers)
Commits related to this issue
- Experimental feature for allowing importing Typescript files outside of the root directory (#22867) This PR attempts to provide an option to allow importing TS/TSX from outside of the current Next.js... — committed to vercel/next.js by shuding 6 years ago
- Experimental feature for allowing importing Typescript files outside of the root directory (#22867) This PR attempts to provide an option to allow importing TS/TSX from outside of the current Next.js... — committed to blitz-js/next.js by shuding 6 years ago
If anyone needs this right now, it is available as an experimental feature in
10.1.2.An example
next.config.jswould beAnother use case of this would be to have a monorepo where mobile and web project sharing the same logic, but without making that sharable code a “repo” itself. Reason for doing that is some tools (react native) don’t like symlinks created by yarn workspaces, or multiple instances of the same dependency (e.g. react, react-native) without symlinks. So it could make things a lot easier to just have a folder of files sitting outside of any repo, and just grab them if you need them by
import { Auth } from '../shared/components'.@lbittner-pdftron we’re not going to allow that, this has been discussed many times before: https://github.com/zeit/next.js/issues/4315
Hello! Next.js does not support importing assets from outside of your base directory.
This includes JavaScript files, which won’t be transformed by Babel.
Maybe we can add a slightly friendlier error message here.
@timneutkens congrats! This long awaited feature finally works BUT it does not recompile on change of above-cwd files.
Is it important? IDK man. I’d say yes and you should make it a top prio on your roadmap. fwiw, we just trashed Next for all of our packages except some dumb seo/leadgen sites because of the subpar interoperability with other systems.
With proper code sharing, being DRY and multiple front end entities you just cant put everything into a Next folder (maybe for some hobby sites). Monorepos and project references are the new normal and Next should obey and recompile on change of any imported file within
baseUrl.related https://github.com/vercel/next.js/issues/5666
This has been solved by the new experimental feature. Created a thread for collecting feedback. Please leave feedback there: https://github.com/vercel/next.js/discussions/26420
You can solve this by organising your code with Yarn workspaces.
Config could be a separate
@app/confignpm package that you import in your application. You can then use next-transpile-modules:withTranspileModules({ transpileModules: ['@app/config'] })if you want to use the same transpilation settings as your Next.js application.@jasonsilberman You can use
experimental.externalDir#22867. I’ve been using this config in a monorepo environment for about a month now and have no problems.@Timer Thanks for pointing us to this issue, and nice to hear that generally speaking you are considering integrating
.tsand.tsxfiles from outside root to be a useful feature.We could imagine to help you with this (i.e., to come up with a contribution), however we don’t yet know where to start. Would you mind giving us some hints on what to pay attention to? Given that this story was estimated as a complexity of 5 points, it shouldn’t be too complicated, and if we can help to bring things forward, we would like to do so (or at least try it).
Of course, we can’t promise anything, since we haven’t taken a closer look at the code yet, and hence do not yet have an idea of what exactly needs to be done, but as said, if you can provide us with some kick-start, we would love to do so.
+1 would be super useful for monorepos and DRY
Any update on when something solving this might land? It seems like a pretty big use case to have a monorepo with multiple next.js frontends that want to share code.
I feel like having a “blessed” solution to monorepos and having an example would be super helpful for a lot of people.