I currently have two folders named shared
and project1
within a larger folder called node
. I am in the process of working on multiple Node projects that are independent and are all located within the node
folder.
Within the shared
folder, there is an index.ts file that contains a default export. The outdir
parameter in the tsconfig.json file is set to ./dist
. Once the project is built, the directory structure looks like this:
node/shared/
src/
index.ts
dest/
index.js
tsconfig.json
...
For project1
, the directory structure post-build looks like:
node/project1/
src/
index.ts
dist/
index.js
The purpose of the shared
folder is to hold common utilities that can be shared among the different projects within the node
folder.
However, when I try to import the shared utilities using the statement
import shared from "../../shared/src/index.js"
in the project1/src/index.ts
file, the resulting compiled output in the dest
folder is chaotic:
node/project1/
src/
index.js
dest/
shared/
src/
index.js <- copied from the shared project
project1/
src/
index.js
Nevertheless, this setup is not functional as the node modules that are installed in the shared
project are not accessible in project1
.
My goal is to have a simple run-time environment without duplicating shared code across projects, like so:
node/
shared/
index.js
project1/
index.js <- importing from shared/index
project2/
index.js <- importing from shared/index
Is this feasible, or is converting the shared module into an NPM package the only viable solution?