I am developing a NPM typescript package to code-split from my main application.
The two projects share a root directory, i.e.:
- mylib
- package.json
- tsconfig.json
- src/
- dist/
- myapp
- package.json
- tsconfig.json
- src/
If myapp/package.json
attempts to link mylib
as file:../mylib
, then npm install
succeeds... but the only way I can succesfully import is to use ... from mylib/src/
(otherwise, the tsc
compiler does not recognize the path). However, this import syntax differs from the production version of the package (which would not include src
).
Plus, I need to be able to also develop mylib
against a second app which is not typescript enabled. Including the src
in the import paths would not work in this scenario, as this path would not resolve against the published NPM package when the application is built by the CI.
I came across one solution which does technically work:
cd mylib
cp package.json dist/
cd dist
npm link
cd ../../myapp
npm link mylib
Thus, the npm link
is created to the dist
folder. But it requires copying the package.json
, otherwise the link just goes to mylib
rather than mylib/dist
.
Unfortunately, this quickly falls apart, because my mylib/package.json
has relative import paths:
"react": "file:../myapp/node_modules/react",
Which are necessary in order to preserve only one instance of the React package. Copying the package.json
into the dist/
folder to create the link causes these imports to fail to resolve. Using an absolute path would not work for other developers on my project. Besides, copying the package.json
into the dist/
seems like an anti-pattern.
Edit 1
I tried switching to yarn
, per the comments. It exhibits the exact same behavior as NPM in this regard. Namely, the yarn link
command always points at the folder with package.json
. Again, this forces me to use the src/
folder in my imports, which will not work in a production build pointing at the production NPM packages.