Maybe there is an approach that I don’t know.
I spent a while and tried various methods such as js/ts
with npm link
, git submodules
, tsconfig paths
, symlink
, npm/pnpm/yaml workspace
, npm pack & npm link
, package.json exports
, package.json commonjs/module
, and nx(nrwl)/lerna
. Each approach has disadvantages, and it looks like there are no convenient standards.
Problem:
As a developer,
I want to write code and reuse it.
I want to start a second project and use only the code I need.
I have code, and I need to share it between multiple projects with the ability to update it.
I am looking for clear instructions.
I use JetBrains IntelliJ IDEA as my IDE.
I don’t want to mess or overcomplicate the code with package management, configs, commands, or other techniques that slow down development and introduce too many extra steps.
I can’t use a monorepo because there are tens of projects, and the IDE doesn’t perform well with searching, replacing, etc.
The code looks like:
/unit_domain_project_1
. .git
. package.json
. /src
. . /units # library
. . . /meter.ts # module
. . . /meter.test.ts # unit testing
. . /nodejs
. . . # Node.js code in TS not compilable in browser
. . /browser
. . . # Browser code in TS not compilable in nodejs
/furniture_project_2
. .git
. package.json
. /src
. . table.ts # here I want to reuse only 'units'
/building_project_3 # extra nesting example
. /src
. . house.ts # here I want to reuse only 'table' with 'units/meter'
Workaround:
The best solution I found is to use symlink
like ln -s $ABSOLUTE_PATH_TO_PROJECT_1/units
, but for Docker, it requires extra shell scripting.
Question:
How can I reuse units
?
(Please answer with step-by-step instructions.)
Why These Approaches Are Not Ideal:
(Described below have cross problems)
git submodules
– Requiresgit pull/push
in each project.symlink
– requires shell scriptingcopy ./linked_code_source ./linked_code
with Docker.tsconfig paths
– Does not support relative paths out of parent(base), IDE not resolve deps.package.json exports: {'*./'}
– There is a mess withpackage.json commonjs/module
, I have to compile TypeScript into module JavaScript. I meet problems with dependencies and path resolving. Many hours just to make it work.npm link
– Not working with Docker, requires shell scripting.npm/pnpm/yarn workspace
– Depends on the npm/node version, makes or symlink or copy file, not updating, need a post_install scripting, rebuilding packages.npm pack & npm link
– need updating script.nx(nrwl)/lerna
– In general, I have to follow their folder structure and use build commands, which means I have to split code into libraries and provide configurations andpackage.json
s, introducing complexity that shouldn’t be there.