-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Cannot import Typescript files from outside of 'dir' directory #9474
Comments
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. |
Ok, I didn't realize this. I think it would be nice to be able to have more flexibility in my project structure. One change that would be nice would to be able to specify where the 'pages' directory is, rather than just the whole base directory (I'm sure theres a reason this isn't currently possible - but it would be nice!). Maybe something like:
Thanks anyways! |
@lbittner-pdftron we're not going to allow that, this has been discussed many times before: #4315 |
You can solve this by organising your code with Yarn workspaces. Config could be a separate |
I am getting this same error when using an npm package that is not compiled to js. Any help with this would be great System information |
I agree with @Timer—there should be a much friendlier error message here. Just spent a few hours debugging this... my first instinct was to dig into the TS-related webpack/babel config, which leads on a series of rabbit trails orthogonal to the simple problem of file location |
Another 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 |
next-transpile-modules should do the trick. |
If anyone wants to try to tackle this you can look at how we do CSS here: next.js/packages/next/build/webpack/config/blocks/css/index.ts Lines 152 to 167 in 792a113
|
#13542 solves this issue I think |
It does partially : if tsconfig declares a |
@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 related #5666 |
Still having some issues with this. Please have a look at this issue: #20374 Editing the |
This would be very useful for us as well, as we have a monorepo with different applications where we want to share logic defined within Typescript files between them. |
Please look at this issue for workaround and insight: I have a monorepo with Next.js for the frontend working. |
👍 would be nice to get this fixed. |
+1 would be super useful for monorepos and DRY |
…of the root directory (#22867) This PR attempts to provide an option to allow importing TS/TSX from outside of the current Next.js project root directory. Although this goes against the design decision that no source code should be imported from outside of root and [might bring tons of issues](#19928 (comment)), it will still be helpful in some monorepo use cases. This PR assumes that the external files are following the same language syntax rules and under the same tooling versions as the source code inside your project root. And it's also not allowed to enable the `baseUrl` feature in the external directory (as the project should only have 1 import base URL). X-ref: #9474, #15569, #19928, #20374.
If anyone needs this right now, it is available as an experimental feature in An example module.exports = {
experimental: {
externalDir: true,
},
} |
…of the root directory (vercel#22867) This PR attempts to provide an option to allow importing TS/TSX from outside of the current Next.js project root directory. Although this goes against the design decision that no source code should be imported from outside of root and [might bring tons of issues](vercel#19928 (comment)), it will still be helpful in some monorepo use cases. This PR assumes that the external files are following the same language syntax rules and under the same tooling versions as the source code inside your project root. And it's also not allowed to enable the `baseUrl` feature in the external directory (as the project should only have 1 import base URL). X-ref: vercel#9474, vercel#15569, vercel#19928, vercel#20374.
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. |
@jasonsilberman |
This has been solved by the new experimental feature. Created a thread for collecting feedback. Please leave feedback there: #26420 |
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. |
Bug report
When using a custom server, trying to import a
.ts
file from outside of thedir
directory results in the following error:To Reproduce
Create a different
.ts
file outside of./src
Create a page and try to import the .ts file outside of
./src
Expected behavior
The file is imported correctly with no errors
System information
Additional context
I am using
ts-node
to execute the server file.The text was updated successfully, but these errors were encountered: