Repository navigation
ts-node with project references in a lerna monorepo #897
Description
I'm using lerna with typescript project references for a node application containing two packages. Package lib and package app (which depends on lib).
I've configured typescript project references so that package lib is built automatically whenever we run tsc --build inside package app (i.e. to build for production).
Here's how the tsconfig.json files are configured for each of the packages:
packages/lib/tsconfig.json:
{
"compilerOptions": {
"strict": true,
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/.tsbuildinfo",
"composite": true
}
}
packages/app/tsconfig.json:
{
"compilerOptions": {
"strict": true,
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/.tsbuildinfo",
"incremental": true
},
"references": [
{ "path": "../lib" }
]
}
Currently, running tsc --build (inside app) compiles typescript to javascript in the dist directory in each of app and lib perfectly fine, the setup runs flawlessly in production mode.
The problem, however, is that trying to run the project in development with ts-node via nodemon --exec 'ts-node src/index.ts' in development fires the following error:
/path/to/packages/app/node_modules/ts-node/src/index.ts:245
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts(1,23): error TS2307: Cannot find module '@myproj/lib'.
...
What seems to be happening, is that ts-node is looking for the @myproj/lib package inside node_modules directory (symlinked by lerna), instead of compiling it on the fly through typetcript's project references, as is setup inside both tsconfig.json files.
I validated my theory by:
- Running a regular
tsc --buildfirst (which also builds thelib/distcode). - Then running
nodemon --exec 'ts-node src/index.ts'again, and it ran fine then.
Which means that ts-node in this case is loading lib via the compiled .js code inside lib/dist (symlinked by lerna), NOT via compiling its .ts code on the fly (via references).
I'm using ts-node@8.4.1 (currently the latest version).
Some questions:
-
Doesn't
ts-nodecurrently support project-references yet or passing the--buildflag totscyet? -
Am I doing something wrong in my (e.g.
tsconfig.json) configurations that's causingts-nodenot to compile/buildlib. -
I can see that a new
--buildflag is being added tots-nodein themasterbranch (commit), but it seems that it's irrelevant totsc's--buildflag.
Note: Currently I'm working around this (without ts-node) via nodemon --exec 'tsc --build && node dist/index.js' till I get this figured out.
Thanks!
Activity
[-]ts-node with typescript project references[/-][+]ts-node with typescript project references in a lerna monorepo[/+][-]ts-node with typescript project references in a lerna monorepo[/-][+]ts-node with project references in a lerna monorepo[/+]blakeembrey commentedon Oct 16, 2019
--emitto clarify the use-case. However, I run into issues with project references on the latestmasterbuild that need to be fixed (so your example might help me too) 😦kerdany commentedon Oct 16, 2019
Thanks @blakeembrey
Thanks!
kerdany commentedon Oct 17, 2019
Hi again,
I've created code to reproduce issue here:
https://github.com/kerdany/ts-node_issue-897
blakeembrey commentedon Oct 31, 2019
@kerdany Thanks! Interestingly I get the same error in development with VS Code and
tsc, until I enable--build. This makes sense, since I assume that TypeScript is actually buildingreferencesfirst when you specify--build. It does make it trickier forts-nodethough, I guess it should also build those dependencies somehow. We might have to do something in memory to make this possible...blakeembrey commentedon Oct 31, 2019
Another interesting part of this is that it's related to
nodeand TypeScript's understanding of module resolution. You can actually make this work with the current release ofts-nodeby importing directly the other.tsfile:Edit: Don't expect this to work in the next major version though, I've been having a lot of trouble with getting project references working and this also fails with VS Code
tsc.jomi-se commentedon Dec 4, 2019
Have you done any progress or got any clues for this? I just ran into the same issue as well
impaler commentedon Dec 7, 2019
I also ran into this trying out packages in yarn workspaces. When wanting to debug with
node -r ts-node/registeras a work around for now I am composing a root tsconfig.json with all relevantreferencesand running atsc --build --watchrunning separately. Would be great to have project references resolve and build with ts-node alone.nickzelei commentedon Jan 10, 2020
FYI it looks like they've added API support for building project references.
microsoft/TypeScript#31432
krzkaczor commentedon Jan 28, 2020
FYI: the obvious workaround is to set
TS_NODE_TRANSPILE_ONLY=truebut then you won't get any type checks.dchambers commentedon Jan 30, 2020
@krzkaczor, I tried setting this on the command line (using
--transpile-onlyand-T) and it makes no difference -- project references still aren't built.dchambers commentedon Feb 10, 2020
I ended up using
concurrentlyto build the project references for all the libraries within our monorepo (all inside alibdirectory) like so:Here, the
libdirectory contained atsconfig.jsonwith references to all the composite projects within the directory.17 remaining items
zomars commentedon Jul 20, 2022
This is what we're doing at @calcom
https://github.com/calcom/cal.com/blob/d1d467d28d03811b50ec2942c8eeef82e2e425a5/packages/tsconfig/base.json#L20-L28
fix(cli): Update watch script: TypeStrong/ts-node#897
fix(cli): Update watch script: TypeStrong/ts-node#897 (#108)
fix(cli): Update watch script: TypeStrong/ts-node#897 (#108)
nicoabie commentedon Aug 8, 2022
thanks @zomars !!!
I was having issues with a custom typing and the
files: trueflag solved the issue.This is my tsconfig.json of the test folder that references the source and has to reference a custom typing the source uses:
Configure TypeScript project references
Configure TypeScript project references
Configure TypeScript project references
pjanickovic-ph commentedon Aug 4, 2023
Hello, I was searching for a solution for like 2 weeks, and I think I found a perfect workaround. the whole idea is that I run my main app using nodemon and ts-node, and except watching my app folder, i added another folder to watch but not the src folder of my library but the build (output folder) and extended the watch to .js files as well. Then, run tsc -b -w on my library which watches the library itself, and rebuild if necessary, and if it emits new .js files, nodemon catches it as well. and it is even in the right order
vorant94 commentedon Aug 31, 2023
so assuming you have like 3 different libs and 1 app in your monorepo... do you start basically 4 processes each time you start coding (3
tsc -b -wand onenodemon)?also this workaround means that each change in the lib would be propagated within 2 rebuilds (1 of the lib itself and 1 of the app that is using the lib), but the changes in the app would be propagated within 1 rebuild, which means inconsistency of the time interval between saving changes and ability to run the code...
it indeed does the job but not without tradeoffs
daniel-savu commentedon Oct 2, 2023
Ran into this issue today as well, although I'm just using yarn workspaces rather than lerna. Maybe worth changing the issue title since this is a broader problem? I've reproduced with a minimal setup here if it helps anyone: https://github.com/daniel-savu/ts-node-bug
gemyago commentedon Nov 1, 2023
I have an npm workspaces and have same issue
JeffJassky commentedon Mar 5, 2024
Anybody have any insight into this as of March 2024? Is this supported yet? Thanks!
vorant94 commentedon Mar 6, 2024
I ended up avoiding ts-node and other typesctipt runners. Here is an example repo for how achieve live-reload and so on solely with
nodebuilt-in watch mode andtschttps://github.com/vorant94/typescript-monorepo
adrian-gierakowski commentedon Mar 6, 2024
https://www.npmjs.com/package/tsc-watch can be used as well for tsc driven workflows