In my workspace, I have a library with two projects: one for the library itself and another for a test application.
├── projects
├── midi-app
└── midi-lib
Within the workspace's tsconfig.json
file, I set up paths for @app
and @lib
:
"paths": {
"@app/*": ["projects/midi-app/src/app/*"],
"@lib/*": ["projects/midi-lib/src/lib/*"],
"midi-lib": [
"dist/midi-lib"
],
"midi-lib/*": [
"dist/midi-lib/*"
]
}
In addition, there is a
projects/midi-lib/tsconfig.lib.json
file that extends the main tsconfig.json
settings:
"extends": "../../tsconfig.json",
The library has a public-api.ts
file which includes:
export * from './lib/midi-lib.module';
I can successfully use this library in the test application. However, when I try importing it as a Node module into a separate client application within a different workspace, I encounter numerous errors related to unknown paths like Can't resolve '@lib/...'
How can I make sure the library paths are accessible in a client application? Or how can I handle the translation of these paths during the library packaging process?
Additionally, I am curious why the extension is not the other way around - why doesn't the main tsconfig.json
file extend the
projects/midi-lib/tsconfig.lib.json
file instead?
Here is my approach to packaging and using the library:
To package the library, I add the following scripts to the parent package.json
file:
"copy-license": "cp ./LICENSE.md ./dist/midi-lib",
"copy-readme": "cp ./README.md ./dist/midi-lib",
"copy-files": "npm run copy-license && npm run copy-readme",
"build-lib": "ng build midi-lib",
"npm-pack": "cd dist/midi-lib && npm pack",
"package": "npm run build-lib && npm run copy-files && npm run npm-pack",
After adding these scripts, I run the command: npm run package
Then, I install the dependency by running:
npm install ../midi-lib/dist/midi-lib/midi-lib-0.0.1.tgz
And import the module in the application module. In the app.module.ts
file:
import { MidiLibModule } from 'midi-lib';
@NgModule({
imports: [
MidiLibModule
Lastly, I insert the component in a template:
<midi-midi-lib></midi-midi-lib>
Upon installing the library in a client application, I notice various .d.ts
files under the node_modules/midi-lib
directories:
├── bundles
├── esm2015
│ └── lib
│ ├── device
│ ├── keyboard
│ ...
│ └── upload
One example is the lib/service/melody.service.d.ts
file:
import { SoundtrackStore } from '@lib/store/soundtrack-store';
import { ParseService } from '@lib/service/parse.service';
import { CommonService } from './common.service';
export declare class MelodyService {
private soundtrackStore;
private parseService;
private commonService;
constructor(soundtrackStore: SoundtrackStore, parseService: ParseService, commonService: CommonService);
addSomeMelodies(): void;
private addSoundtrack;
private generateNotes;
}
This file references the @lib
path mapping, which is not recognized in the client application.
I also tried using the baseUrl
property as a workaround, but it didn't solve the issue since the baseUrl
value was not specified during library installation.
Why does running npm run package
to package the library not resolve the paths
mappings?