You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
create-react-app should allow TypeScript imports outside src #8785
Code reuse is a good thing. It is not uncommon that a backend and a frontend can use the same code, and live in the same repository. The need for sharing code is particularly strong with TypeScript type definitions, because developers on the backend and the frontend want to make sure that they are building against a common types.
Describe the solution you'd like
I don't know the CRA internals, but maybe one of the following:
Allow baseUrl to point outside . to enable absolute imports like `import * as foo from 'common/foo``.
Enable relative imports outside src like import * as foo from '../../common/foo'.
Describe alternatives you've considered
There are work-arounds using a combination of third-party cra-patching libraries. They are tedious to setup though (I worked 3 evenings on coming up with a solution), potentially fragile, and the use case seems so fundamental that I'm very surprised that it is not to supported out-of-the-box.
Hi @bluenote10
You can disable this feature but only after eject operation of create-react-app project or use react-app-rewired package this way you do not have to eject.
I know that it is possible, as I have documented in the link.
However the process to get there is out of proportion. The area of working against the CRA defaults is quite a mess for a newbie, and because the settings of CRA are hidden, documentation is poor. I worked on the solution for 3 evenings, roughly 3 hours each. Isn't it crazy to be stuck for such a long time on a basic configuration problem, not doing anything productive? I thought the idea of CRA was to get people going quickly. After spending 9 hours on solving a problem that seems to be a basic requirement and facing limitations that have no clear motivation at all, I thought I report my experience.
Reacted by strblr, RaffBadura, Ryota Murakami, Matthew Keller, bchoii, Alan Jereb, Pedro Morello Abbud, Lucas Molin, Manny Alvarado, S V and 36 more
+1, typescript should be enabled for outside the src folder by default. Or at least there has to be a way to import typescript dependencies by relative path in package.json - which seems to be currently impossible to do, without ejecting and pretty much losing any benefit of cra.
I agree that code sharing should be encouraged and easier as long as the import does not leave the repository.
Also related to #1333 which has yet to receive any update from a CRA maintainer.
Reacted by Baltazar Gomez, Sean Blonien, Edoardo Gallo, Djangoliv, nickfreeman, strblr, Ryota Murakami, Kai J Arnold and Felix Flügel
+1, I would say this is one of the deal-breakers for us, we have lots of shared components and utility functions/classes, and sharing them across multiple CRA projects has been more than painful.
Ofc we can hack our way through this, using things like react-app-rewired or craco, but it's not an ideal solution, and I'm curious to know why is this not supported out of the box?
Reacted by strblr, Heath, Ryota Murakami, S V, Barry G, Kai J Arnold, Marcus Wyremblewski and George Filippou
Is it explained somewhere why the restriction on not importing things from outside src/ exists? What do the CRA maintainers recommend for sharing e.g. TypeScript files with the frontend?
Reacted by Kai J Arnold, joelwestland and Drazen Bjelovuk
@erjiang Because ModuleScopePlugin working inside webpack.
This code block is setting on webpack config file that disable include file from outside src/.
Original motivation is here
// Prevents users from importing files from outside of src/ (or node_modules/).
// This often causes confusion because we only process files within src/ with babel.
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
// please link the files into your node_modules/ and let module-resolution kick in.
// Make sure your source files are compiled, as they will not be processed in any way.
-- frontend-app1
|
--frontend-app2
|
--domain
|
----some common logic
With above setup I want to work on both apps with shared logics inside domain directory. It would be hard to rebuild this logic and setup it as package, then install from package manager in both apps - just to have it in node_modules directory.
Is there any chance that this option will be at least considered?
It should be noted that this TypeScript issue is a bit of a showstopper for enabling imports outside src.
Basically TypeScript has a bug (or design limitation?) that allows it to import from a "foreign" node_modules folder. This can have nasty consequences: For instance you forget to specify dependency foo in your package.json in your "frontend-app1". But in "frontend-app1" you make an import to "domain" which uses foo. If "domain" contains a node_modules folder TypeScript may accidentally use foo from there. I.e., you can end up in situations where the dependencies in "frontend-app1" are incomplete without realizing. Or it could cause headaches, because you assume you're using version X as specified in the local package.json but you're rather using version Y from that foreign node_modules.
Ideally that TypeScript issue should be fixed first to allow for sensible cross-folder imports.
For instance you forget to specify dependency foo in your package.json in your "frontend-app1". But in "frontend-app1" you make an import to "domain" which uses foo. If "domain" contains a node_modules folder TypeScript may accidentally use foo from there. I.e., you can end up in situations where the dependencies in "frontend-app1" are incomplete without realizing. Or it could cause headaches, because you assume you're using version X as specified in the local package.json but you're rather using version Y from that foreign node_modules.
That actually sounds like a good argument. However, there could be an easier way to tell TS that I eventually want to hurt myself and import something outside src directory.
+1, typescript should be enabled for outside the src folder by default. Or at least there has to be a way to import typescript dependencies by relative path in package.json - which seems to be currently impossible to do, without ejecting and pretty much losing any benefit of cra.
import {
CommerceContextProvider,
PrimeCatalogContainer,
PrimeCommunityBoardList,
PrimeCommunityBoardPage,
PrimeInstancePage,
PrimeNotificationContainer,
PrimeTrainingPage,
} from "@almLib/almLib";
here @almLib is declared in package.json
and still shows error
My goal is to access components from almLib which is outside of src
@ttaranov solution works well in 2022 for sharing a common logic folder between react and react-native projects in the same repo. Only JS and no JSX in there though - not sure if that would work. Thank you for this hack! Super valuable to stay on create-react-app.
Please note that this will copy your shared directory to node_modules, so changes to this directory are not reflected. We ended up not using this solution.
@erjiang Because ModuleScopePlugin working inside webpack. This code block is setting on webpack config file that disable include file from outside src/.
Original motivation is here
// Prevents users from importing files from outside of src/ (or node_modules/).
// This often causes confusion because we only process files within src/ with babel.
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
// please link the files into your node_modules/ and let module-resolution kick in.
// Make sure your source files are compiled, as they will not be processed in any way.
This sounds like a joke. What if we want to reference a file that doesn't need any babel processing? Also, it sounds dumb to use eject solution only for this.
I am also encountering similar issues to others in the threads, and the hacky solutions proposed did not work for me. This is definitely an important ask
Reacted by Bartłomiej Kalemba, Jan Neuman, Sasha Sirotkin, JC Louis, jazozaya, Alexandros Dorodoulis, bozidar siljanoski, Landon Gavin, kleydon, Frank Edwards and 4 more
I am currently working on an app using Electron and React.
I am in a situation right now where I have several enums and other data/functions that need to be shared between the src and electron folders, but this is not possible.
This has led to me copy pasting the same data/functions to separate directories and having to update both when a change is made. Horrendous.
I just tried doing a whole project in Vite and it does this perfectly among other things, if you want a lightweight OOTB-usable framework for React that has hot reload and produces static assets I think it's the reasonable choice.
Reacted by pegasusdesignsystem, dmoughabghab and Oleg Glybchenko
Hi guys I don't know if this might be helpful to you, but I published this npm package so I could "inject" the code inside a CRA project rather than eject itself.
This solution has few issues though:
I lose the hot-reload of the component I want to inject. I had to build everytime I make a change (or I could work inside the lib folder for small changes, then rewrite them into the ouside folder "src"),
[less important] If there are any other files (such as .svg) inside the src, I had to specify inside the "build" script.
The main problem was the hot-reload for me. That's why I was searching for other solutions. But maybe this solution can help someone
I just tried doing a whole project in Vite and it does this perfectly among other things, if you want a lightweight OOTB-usable framework for React that has hot reload and produces static assets I think it's the reasonable choice.
This reply helped a lot, seems the world is moving away from create react app, this works by default in Vite
Francois,
Thank you for this feedback. Yes unfortunately CRA is not what it used to
be and we found it very issue prone so now we do suggest Vite.
I'll pass this info alone to engineering. Thank you!
Activity
getspooky commentedon Apr 6, 2020
Hi @bluenote10
You can disable this feature but only after eject operation of create-react-app project or use react-app-rewired package this way you do not have to eject.
bluenote10 commentedon Apr 7, 2020
I know that it is possible, as I have documented in the link.
However the process to get there is out of proportion. The area of working against the CRA defaults is quite a mess for a newbie, and because the settings of CRA are hidden, documentation is poor. I worked on the solution for 3 evenings, roughly 3 hours each. Isn't it crazy to be stuck for such a long time on a basic configuration problem, not doing anything productive? I thought the idea of CRA was to get people going quickly. After spending 9 hours on solving a problem that seems to be a basic requirement and facing limitations that have no clear motivation at all, I thought I report my experience.
timtaranov commentedon Apr 25, 2020
+1, typescript should be enabled for outside the src folder by default. Or at least there has to be a way to import typescript dependencies by relative path in package.json - which seems to be currently impossible to do, without ejecting and pretty much losing any benefit of cra.
I'd think this should work even if the mycore dependency above is in written in typescript
CodingDive commentedon Jun 10, 2020
I agree that code sharing should be encouraged and easier as long as the import does not leave the repository.
Also related to #1333 which has yet to receive any update from a CRA maintainer.
xinghul commentedon Oct 3, 2020
+1, I would say this is one of the deal-breakers for us, we have lots of shared components and utility functions/classes, and sharing them across multiple CRA projects has been more than painful.
Ofc we can hack our way through this, using things like
react-app-rewiredorcraco, but it's not an ideal solution, and I'm curious to know why is this not supported out of the box?timtaranov commentedon Oct 3, 2020
just to document react-app-rewired/customize-cra workaround for anyone looking for a solution:
react-app-rewiredandcustomize-cra andreplace script build/start/test eact-app-rewired build/start/testconfig-overrides.jsinto cra project's root withwith these changes, CRA starts detecting and building typescript codebases outside of the project
srctree.bluenote10 commentedon Nov 27, 2020
@ttaranov react-app-rewired stopped supporting CRA at version 2.0 and now we are at 4.0, so isn't this a very fragile setup?
ryota-murakami commentedon May 21, 2021
@bluenote10 I had facing this problem lately, and I agree with you totally.
So I've just submit Add DISABLE_MODULE_SCOPE_PLUGIN option PR.
ModuleScopePlugin restricts import file outside
/srcinternally, therefore we could import from everywhere if you disable it.erjiang commentedon Jul 13, 2021
Is it explained somewhere why the restriction on not importing things from outside src/ exists? What do the CRA maintainers recommend for sharing e.g. TypeScript files with the frontend?
ryota-murakami commentedon Jul 14, 2021
@erjiang Because
ModuleScopePluginworking inside webpack.This code block is setting on webpack config file that disable include file from outside
src/.Original motivation is here
quote from #2189
sculpt0r commentedon Aug 19, 2021
I have a repo with structure like:
-- frontend-app1
|
--frontend-app2
|
--domain
|
----some common logic
With above setup I want to work on both apps with shared logics inside domain directory. It would be hard to rebuild this logic and setup it as package, then install from package manager in both apps - just to have it in node_modules directory.
Is there any chance that this option will be at least considered?
bluenote10 commentedon Aug 19, 2021
It should be noted that this TypeScript issue is a bit of a showstopper for enabling imports outside src.
Basically TypeScript has a bug (or design limitation?) that allows it to import from a "foreign"
node_modulesfolder. This can have nasty consequences: For instance you forget to specify dependencyfooin yourpackage.jsonin your "frontend-app1". But in "frontend-app1" you make an import to "domain" which usesfoo. If "domain" contains anode_modulesfolder TypeScript may accidentally usefoofrom there. I.e., you can end up in situations where the dependencies in "frontend-app1" are incomplete without realizing. Or it could cause headaches, because you assume you're using version X as specified in the localpackage.jsonbut you're rather using version Y from that foreignnode_modules.Ideally that TypeScript issue should be fixed first to allow for sensible cross-folder imports.
sculpt0r commentedon Aug 19, 2021
That actually sounds like a good argument. However, there could be an easier way to tell TS that I eventually want to hurt myself and import something outside
srcdirectory.3 remaining items
norayr93 commentedon Apr 22, 2022
@lingdocs Only removing that part will not solve the issue as I am struggling a few days to come up with a solution.
Bhavana1233 commentedon Jun 16, 2022
no its not working for me
Bhavana1233 commentedon Jun 16, 2022
import {
CommerceContextProvider,
PrimeCatalogContainer,
PrimeCommunityBoardList,
PrimeCommunityBoardPage,
PrimeInstancePage,
PrimeNotificationContainer,
PrimeTrainingPage,
} from "@almLib/almLib";
here @almLib is declared in package.json
and still shows error
My goal is to access components from almLib which is outside of src
nikodunk commentedon Jul 3, 2022
@ttaranov solution works well in 2022 for sharing a common logic folder between react and react-native projects in the same repo. Only JS and no JSX in there though - not sure if that would work. Thank you for this hack! Super valuable to stay on create-react-app.Please note that this will copy your shared directory to node_modules, so changes to this directory are not reflected. We ended up not using this solution.
cristiantobol commentedon Jul 25, 2022
This sounds like a joke. What if we want to reference a file that doesn't need any babel processing? Also, it sounds dumb to use eject solution only for this.
thomasgauvin commentedon Sep 1, 2022
I am also encountering similar issues to others in the threads, and the hacky solutions proposed did not work for me. This is definitely an important ask
Ezekias1337 commentedon Oct 27, 2022
I am currently working on an app using Electron and React.
I am in a situation right now where I have several enums and other data/functions that need to be shared between the src and electron folders, but this is not possible.
This has led to me copy pasting the same data/functions to separate directories and having to update both when a change is made. Horrendous.
kleydon commentedon Nov 19, 2022
+1.
Significant limitation of CRA; looking forward to a resolution.
lbfalvy commentedon Nov 19, 2022
I just tried doing a whole project in Vite and it does this perfectly among other things, if you want a lightweight OOTB-usable framework for React that has hot reload and produces static assets I think it's the reasonable choice.
pegasusdesignsystem commentedon Nov 20, 2022
When is this change going to be made to allow outside of /src? Any ETA?
emanuelecaurio commentedon Dec 14, 2022
Hi guys I don't know if this might be helpful to you, but I published this npm package so I could "inject" the code inside a CRA project rather than eject itself.
This solution has few issues though:
The main problem was the hot-reload for me. That's why I was searching for other solutions. But maybe this solution can help someone
dmoughabghab commentedon Mar 6, 2023
This reply helped a lot, seems the world is moving away from create react app, this works by default in Vite
pegasusdesignsystem commentedon Aug 1, 2023
KrunalParmar85 commentedon Nov 29, 2023
I am having similar issue, but it occurs only when I build application from Linux container. When I build from my local widow machine it works fine.
is it some environment issue?
Also how does it allow us to import controls from node_modules folder which is outside SRC directory as well.