简体   繁体   English

Angular 在 tsconfig.json 中声明的库路径别名不起作用:找不到模块:错误:无法解析

[英]Angular Library paths alias declared in tsconfig.json not working: Module not found: Error: Can't resolve

I'am developing an Angular (v12) library and a small app to test it that have the following structure:我正在开发一个 Angular (v12) 库和一个小应用程序来测试它,它具有以下结构:

├── projects
│   ├── my-app
│   │   ├── karma.conf.js
│   │   ├── src
│   │   ├── tsconfig.app.json
│   │   └── tsconfig.spec.json
│   └── my-lib
│       ├── README.md
│       ├── karma.conf.js
│       ├── ng-package.json
│       ├── node_modules
│       ├── package-lock.json
│       ├── package.json
│       ├── src
│       ├── tsconfig.lib.json
│       ├── tsconfig.lib.prod.json
│       └── tsconfig.spec.json
└── tsconfig.json

In the root./tsconfig.json I declare paths aliases (as I understand to be recognized by VS Code):在 root./tsconfig.json 我声明路径别名(据我所知,VS Code 可以识别):

 {
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "paths": {
      "my-lib": [
        "dist/my-lib/my-lib",
        "dist/my-lib"
      ],
      "@services/*": [
        "projects/my-lib/src/lib/services/*"
      ]
    },
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",
    "module": "es2020",
    "lib": [
      "es2020",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

In the./projects/my-lib/tsconfig.lib.json I declare again the paths aliases (as I understand to be interpreted by the ng-packagr compiler):在 ./projects/my-lib/tsconfig.lib.json 我再次声明路径别名(据我所知由 ng-packagr 编译器解释):

{
    "compileOnSave": false,
    "compilerOptions": {
      "baseUrl": "./",
      "outDir": "./dist/out-tsc",
      "paths": {
        "@services/*": [ "src/lib/services/*" ],
      },
      "sourceMap": true,
      "declaration": false,
      "module": "es2015",
      "moduleResolution": "node",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "target": "es5",
      "typeRoots": [
        "node_modules/@types"
      ],
      "lib": [
        "es2017",
        "dom"
      ]
    }
  }

The./angular.json is referencing the tsconfig.lib.json: ./angular.json 引用的是 tsconfig.lib.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-lib": {
      "projectType": "library",
      "root": "projects/my-lib",
      "sourceRoot": "projects/my-lib/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:ng-packagr",
          "options": {
            "project": "projects/my-lib/ng-package.json"
          },
          "configurations": {
            "production": {
              "tsConfig": "projects/my-lib/tsconfig.lib.prod.json"
            },
            "development": {
              "tsConfig": "projects/my-lib/tsconfig.lib.json"
            }
          },
          "defaultConfiguration": "production"

The Library is compiling without any errors.库正在编译,没有任何错误。

When I import the library in my-app and I want to import a service for example, It does not resolve the paths aliases:例如,当我在 my-app 中导入库并且想要导入服务时,它不会解析路径别名:

./node_modules/my-lib/fesm2015/my-lib.mjs:11:0-56 - Error: Module not found: Error: Can't resolve '@services/my-lib/my-lib.service' in '/Users/***/projects/ANGULAR/angular-tour-of-heroes/node_modules/my-lib/fesm2015'
    
    Error: node_modules/my-lib/lib/custom-page/custom-page.component.d.ts:2:30 - error TS2307: Cannot find module '@services/my-lib/my-lib.service' or its corresponding type declarations.
    
    2 import { MyLibService } from '@services/my-lib/my-lib.service';

Error: node_modules/my-lib/lib/custom-page/custom-page.component.d.ts:2:30 - error TS2307: Cannot find module '@services/my-lib/my-lib.service' or its corresponding type declarations.

2 import { MyLibService } from '@services/my-lib/my-lib.service';

I tried any combinations of configuration in the world, paths seems to be impossible to be used in an Angular Library我尝试了世界上任何配置组合,路径似乎不可能在 Angular 库中使用

The tsconfig file for production is projects/my-lib/tsconfig.lib.prod.json (as declared in angular.json), so you probably have forgotten to set the alias path for production.生产环境的 tsconfig 文件是projects/my-lib/tsconfig.lib.prod.json (在 angular.json 中声明),所以你可能忘记设置生产环境的别名路径。 To my understanding, this should point to the right path in the dist folder.据我了解,这应该指向dist文件夹中的正确路径。

That said, I have the same issue (but already when running dev locally), so this is just a guess.也就是说,我有同样的问题(但已经在本地运行 dev 时),所以这只是一个猜测。

Edit:编辑:

I got it working locally, and am now facing a similar issue when trying to build.我让它在本地工作,现在在尝试构建时面临类似的问题。 In my case it's an error not an absolute path (no ref to where that error comes from), and when I use the alias everywhere in my components, a lot of No suitable injection token for parameter 'tooltip' of class 'MyComponent'.在我的情况下,这是一个错误not an absolute path (没有参考该错误的来源),并且当我在组件中的任何地方使用别名时No suitable injection token for parameter 'tooltip' of class 'MyComponent'. errors.错误。

I still hope you can make it work, but perhaps my answer won't lead to the solution.我仍然希望你能让它工作,但也许我的回答不会导致解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular/Typescript tsconfig.json 配置多个路径的路径别名 - Angular/Typescript tsconfig.json configure path alias with multiple paths 在 tsconfig.json 上使用路径无法使用 angular 解析 - using path on tsconfig.json doesn't resolve using angular 路径在tsconfig.json中无法正常工作 - paths is not working in tsconfig.json as expected Angular 9 tsconfig.json & tsconfig.app.json 路径未按预期工作 - Angular 9 tsconfig.json & tsconfig.app.json paths are not working as expected 在角度 10 的 tsconfig.json 中找不到 typeRoots - typeRoots not found in tsconfig.json in angular 10 将 Angular 10 项目转换为 nativescript 共享项目时出现“无法解析 tsconfig.json 文件”错误 - “Can't parse tsconfig.json file” error when converting Angular 10 project to nativescript shared project Angular Typescript - exclude在tsconfig.json中不起作用 - Angular Typescript - exclude is not working in tsconfig.json VSCode / tslint 不解析 tsconfig.json 中的路径 - VSCode / tslint does not resolve paths in tsconfig.json 在配置文件“ tsconfig.json”中找不到输入。 指定的“包含”路径在Angular 6中 - No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were in Angular 6 Visual Studio Code 无法解析角度的 tsconfig 路径 - Visual Studio Code can't resolve angular's tsconfig paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM