I have a project with 3 directories client
, api
, and shared
. The shared
directory contains typescript types and definitions inside an engine
folder that I would like to share with client
and api
. Additionally, the shared
directory requires some third party dependencies as well (specified in its package.json file).
A very basic sample repository describing this scenario can be found here, under the branch project-references
. Essentially, server.js
in api
will import and call a function from the Entity.ts
class which in turn, relies on an external library (mathjs).
Now I have been trying to use project references (rather unsuccessfully!) to build my api
(I have not looked into the client yet!) so that the shared code will be available in a dist
folder within the api
for use.
What I have done so far:
- I have added the following
tsconfig.build-api.json
file toshared
directory. TheoutDir
points to adist
folder insideapi
. The idea is to move the compiled shared files to the api'sdist
folder, maintaining the same directory structure indist
so that my imports won't break. Similarly, I plan on having another tsconfig file for the client.
{
"compilerOptions": {
"lib": ["es2015", "es2017", "dom"],
"module": "commonjs",
"target": "es6",
"declaration": true,
"declarationMap": true,
"composite": true,
"moduleResolution": "node",
"outDir": "../api/dist/shared"
},
"include": ["engine"],
"exclude": ["node_modules", "lib"],
"references": []
}
- I have updated the api's
tsconfig.json
file to reference the shared directory'stsconfig.build-api.json
file
{
"compilerOptions": {
// some options
"outDir": "dist/api/src",
},
"references": [
{ "path": "../shared/tsconfig.build-api.json" }
],
// some more options
}
- I run
tsc --build .
from inside theapi
directory which creates adist
folder with the compiled api and shared code in it. However, when I try to run this built code, I get an error stating that the third party dependencies of theshared
code cannot be found.
How can I make sure the third party dependencies of my referenced project (shared
) are resolved without any issues inside api
?
Note: Without the third party dependencies in my shared code, I could get all of this working without project references by just using relative paths to do the required imports. Thiw was explained here. However, with the external dependencies, I still cannot figure out a way to do this. Research has led to things like monorepositories, learna, yarn workspaces etc etc. But I would like to think this can be solved without relying on external tooling.