I have set up an example project to demonstrate the issue:
https://github.com/garethrbrown/node-share-code
In this example there are two projects, example-api (a mini express project) and example-shared (a class library), both using Node JS / TypeScript. I want example-api to be able to use classes from example-shared to avoid code duplication. Having followed this example, I have referenced the example-shared project from package.json
in example-api.
"dependencies": {
"example-shared": "file:..\\example-shared",
"express": "^4.17.1"
}
Having done this, and following running npm install
, intellisense in VSCode sees ApiClass
from the example-shared project and assists with the import.
I can then run by build command tsc --build
via NPM, which succeeds.
I can also see that the sym link has been created in the example-api node_modules
directory.
However, when I try to run the example-api project using the npm start
script from under example-api, I get an error along the lines of:
Error: Cannot find module 'example-shared/apiClass'
Require stack:
...
code: 'MODULE_NOT_FOUND',
requireStack: [
...
]
I have tried running commands from different locations such as described here, but with no luck so far.
I'm using current stable versions of Node (14+) and NPM (7+).
I don't want to share via NPM or git repositories as I feel it will slow down development.
What am I doing wrong?