Angular组件包发布时报错Entry point不存在该如何解决?
问题产生原因
- 服务未完成Angular打包需要的注册逻辑:你定义的
LocaleService未在@Injectable装饰器中指定providedIn属性,也未添加到库对应NgModule的providers数组中,Angular库默认打包工具ng-packagr不会将未注册的服务纳入打包产物,因此提示对应入口点不存在。 - 公共导出路径不匹配:你在api导出文件中写的
./src/locale.service路径和LocaleService的实际存放路径不一致,Angular库的业务源码默认放在lib目录下而非直接放在src根目录,路径错误会导致打包工具找不到对应文件。 - 内部导入路径不规范:如果在
FormComponent中导入LocaleService时使用了深层相对路径,也可能导致ng-packagr解析路径时判定入口点不存在。
修复方案
- 完成服务注册,二选一即可:
方案一:给@Injectable装饰器添加providedIn配置,声明为全局单例服务
方案二:将服务添加到你库的根NgModule的@Injectable({ providedIn: 'root' }) export class LocaleService {}providers数组中@NgModule({ // 原有declarations、imports、exports等配置保持不变 providers: [LocaleService] }) export class ComponentsModule {} - 修正api导出文件的路径,根据服务实际存放路径调整,比如服务存放在
src/lib/locale.service.ts的话,导出代码改为:export * from './lib/locale.service'; - 修正组件内的导入路径,直接从库的公共入口导入服务,不要使用相对路径:
import { LocaleService } from 'swagular/components'; export class FormComponent { constructor(private localeService: LocaleService) {} } - 重新执行打包发布命令即可。
内容的提问来源于stack exchange,提问作者yantrab
相关产品推荐
相关产品推荐

