ng-packagr: TypeScript paths not resolved
Type of Issue
[x] Bug Report
[ ] Feature Request
Description
TypeScript paths do not get resolved when executing ng-packagr -p ng-package.json.
Paths get resolved normally on ng serve(I am using Angular Cli).
How To Reproduce
- clone repository: https://github.com/klemenoslaj/ng-packagr-bug-example
- install dependencies with
yarnornpm install - execute npm script
build:lib
Error
Cannot find module '@example/example'
Expected Behaviour
Should be no error. Imports should work as they work with ng serve.
Version Information
ng-packagr: 1.6.0
@angular/*: 5.2.1
@angular/cli: 1.6.5
typescript: 2.5.3
rxjs: 5.5.6
node: 9.4.0
yarn: 1.3.2
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 6
- Comments: 32 (8 by maintainers)
Hi, this is expected and this is by TypeScript design. Paths don’t get re-rewired when emitted.
Hence, when building a library you should only use paths to change the location of an external module lookup. Not to make an import look clean.
When using nice imports like
@app/fooThis works in an application because of tools such as webpack as running the javascript code directly won’t work as it’s not valid.Ideally you should not output js code that doesn’t rely on tooling to work. Hence I would strongly discourage using
pathsto shorten the import and make them look clean.pathsIdeally should be used to change the resolutions and not to change the module import statement.Add index.ts file to your library folder next to public_api.ts file, and move all exports to index.ts file.
Update public_api.ts file to the following:
This should make both ng-packagr and TypeScript happy.
News on this? I can’t believe there’s no solution!
Same issue
tsconfig.json
“ng-packagr”: “^3.0.6” “typescript”: “2.9.2”
It seems that this is a problem even in the example ng-packaged app. I just cloned it today, did an npm install and the ng serve works fine. But if you open src/app/app.component.ts, the import
import { BarService } from '@my/lib';says[ts] Cannot find module '@my/lib'.. I tried adding the index.ts like you sais @Karankang007 , but I get the same error. This would be very problematic for development of a library if it can’t resolve the components.A temporary solution is to build your librairies without intra package dependencies inside your node_modules. After that, you can build all packages with a reference of your others intra packages.
Any solution on this?
There’s no valid solution to this problem.
Lot’s of hand waving but none of the code in this thread actually works.
Building a library and I’m in relative path hell, all my imports look like this and no way to fix it by using a token defined in tsconfig
pathsimport { VinDecodeService } from '../../../services/vin-decode/vin-decode.service';I tried hundred times to get this import to not suck. Just want it to look like this:
import { VinDecodeService } from 'src/services/vin-decode/vin-decode.service';I tried using
@dynatron/frameworkfor the path token instead ofsrcsince that’s what is defined in package.json, that did not work.Every time VSCode editor try to automatically add an import it screws it up. Have to manually go put in bunch of dot-dot-slash’s, it is real painful coding experience. Hope there is some way to recognize
pathsof tsconfig.You cannot create
pathsintsconfigsand import them unless they are secondary entrypoints, ie they have apackage.jsonand anng-packagrconfiguration.This is because from a consumer perspective it is impossible to resolve such entries and typescript won’t rewrite these during compilation.
Building a library is not the same as building an application.
If you want to reference files which are not relative, you need to create secondary entry points as explained here; https://github.com/dherges/ng-packagr/blob/master/docs/secondary-entrypoints.md
This is to have something like
Not that
@my-liband@my-lib/testingare defined inpackage.jsonthank you,
I am seeing that you are using an old version of ng-package. This was solved in v3+
On Mon, 06 Aug 2018 at 14:48, Bilal ElDabet notifications@github.com wrote:
Hello… Any Update on this issue? I still have Cannot find module @example /example thank you…
Seems like rollup has a similar tool https://github.com/rollup/rollup-plugin-alias
Has anyone tried integrating this?
At my work we have the use case of wanting to integrate various Angular UIs from separate repos into a new app a la micro-frontends, via lazily loaded Angular libraries. All of the UIs have been written to use paths (a best practice), so I will be working on being able to build libraries out of such code, one way or another. It’s a must for us.
Hi, not sure if this is the same issue as it sounds very similar (let me know if it isn’t and I’ll raise another one). “https://github.com/klemenoslaj/ng-packagr-bug-example” is not the same.
In a regular angular project, the “paths” property of tsconfig.json allows you to reference modules using cleaner imports (i.e. control the amount of nesting). Examples commonly seen are @app, and @env. I’m not entirely familiar with how the build is generated, but essentially these paths resolve correctly. They behave as aliases and are interchangeable with relative paths (let me know if this is not the case).
With ng-packagr, I want to use the same path scheme within the library. Adding the aliases (@app) to tsconfig.lib.json allows the library to be built. However the alias path @app remains in the generated fesm5 and fesm2015 javascript files.
Using the lib in a new angular app results in a module not found error. If I were to replace the @app with the equivalent relative path, the fesm5 and fesm2015 appears to ‘inline’ the reference modules.
I do not need or want to expose the internal modules of the library as secondary entry points. It would be good if they can remain internal. However, I could not find a way to do this while still using alias paths (avoiding relative paths) within the library.
Please let me know if that makes sense, and whether it is the same issue being discussed. Even better if you have a solution to this.
Dear @alan-agius4 ,
Here’s a sample of what I’m trying to do: https://github.com/bilaldabet/ngPackgrTest
I think the problem is from my side, can you please Guide me.
Thank you,
@jRichardeau you can use the ng-packagr api directly with a custom tsconfig.
Please look at the example here: https://github.com/dherges/ng-packagr/tree/master/integration/samples/api
That is fine by me. Will change my source after the release. Great, nice work! 👍