OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JS/TS How to Reuse Code Between Projects?

  • Thread starter Thread starter Yevhenii Kamenskyi
  • Start date Start date
Y

Yevhenii Kamenskyi

Guest
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:

Code:
/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 - Requires git pull/push in each project.
  • symlink - requires shell scripting copy ./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 with package.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 and package.jsons, introducing complexity that shouldn't be there.

<p>Maybe there is an approach that I don't know.</p>
<p>I spent a while and tried various methods such as <code>js/ts</code> with <code>npm link</code>, <code>git submodules</code>, <code>tsconfig paths</code>, <code>symlink</code>, <code>npm/pnpm/yaml workspace</code>, <code>npm pack & npm link</code>, <code>package.json exports</code>, <code>package.json commonjs/module</code>, and <code>nx(nrwl)/lerna</code>. Each approach has disadvantages, and it looks like there are no convenient standards.</p>
<p><strong>Problem:</strong><br />
As a developer,<br />
I want to write code and reuse it.<br />
I want to start a second project and use only the code I need.<br />
I have code, and I need to share it between multiple projects with the ability to update it.<br />
I am looking for clear instructions.<br />
I use JetBrains IntelliJ IDEA as my IDE.</p>
<p>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.<br />
I can't use a monorepo because there are tens of projects, and the IDE doesn't perform well with searching, replacing, etc.</p>
<p>The code looks like:</p>
<pre class="lang-bash prettyprint-override"><code>/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'
</code></pre>
<p><strong>Workaround:</strong><br />
The best solution I found is to use <code>symlink</code> like <code>ln -s $ABSOLUTE_PATH_TO_PROJECT_1/units</code>, but for Docker, it requires extra shell scripting.</p>
<p><strong>Question:</strong><br />
How can I reuse <code>units</code>?
(Please answer with step-by-step instructions.)</p>
<p><strong>Why These Approaches Are Not Ideal:</strong>
(Described below have cross problems)</p>
<ul>
<li><code>git submodules</code> - Requires <code>git pull/push</code> in each project.</li>
<li><code>symlink</code> - requires shell scripting <code>copy ./linked_code_source ./linked_code</code> with Docker.</li>
<li><code>tsconfig paths</code> - Does not support relative paths out of parent(base), IDE not resolve deps.</li>
<li><code>package.json exports: {'*./'}</code> - There is a mess with <code>package.json commonjs/module</code>, I have to compile TypeScript into module JavaScript. I meet problems with dependencies and path resolving. Many hours just to make it work.</li>
<li><code>npm link</code> - Not working with Docker, requires shell scripting.</li>
<li><code>npm/pnpm/yarn workspace</code> - Depends on the npm/node version, makes or symlink or copy file, not updating, need a post_install scripting, rebuilding packages.</li>
<li><code>npm pack & npm link</code> - need updating script.</li>
<li><code>nx(nrwl)/lerna</code> - 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 and <code>package.json</code>s, introducing complexity that shouldn't be there.</li>
</ul>
 

Latest posts

M
Replies
0
Views
1
Mohit Pant
M
Top