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 --build first (which also builds the lib/dist code).
- 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-node currently support project-references yet or passing the --build flag to tsc yet?
-
Am I doing something wrong in my (e.g. tsconfig.json) configurations that's causing ts-node not to compile/build lib.
-
I can see that a new --build flag is being added to ts-node in the master branch (commit), but it seems that it's irrelevant to tsc's --build flag.
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!
I'm using
lernawith typescript project references for a node application containing two packages. Packageliband packageapp(which depends onlib).I've configured typescript project references so that package
libis built automatically whenever we runtsc --buildinside packageapp(i.e. to build for production).Here's how the
tsconfig.jsonfiles are configured for each of the packages:packages/
lib/tsconfig.json:packages/
app/tsconfig.json:Currently, running
tsc --build(insideapp) compiles typescript to javascript in thedistdirectory in each ofappandlibperfectly fine, the setup runs flawlessly in production mode.The problem, however, is that trying to run the project in development with
ts-nodevianodemon --exec 'ts-node src/index.ts'in development fires the following error:What seems to be happening, is that
ts-nodeis looking for the@myproj/libpackage insidenode_modulesdirectory (symlinked bylerna), instead of compiling it on the fly through typetcript's project references, as is setup inside bothtsconfig.jsonfiles.I validated my theory by:
tsc --buildfirst (which also builds thelib/distcode).nodemon --exec 'ts-node src/index.ts'again, and it ran fine then.Which means that
ts-nodein this case is loadinglibvia the compiled.jscode insidelib/dist(symlinked bylerna), NOT via compiling its.tscode 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!