【问题标题】:Angular Cannot find module of my custom libraryAngular 找不到我的自定义库的模块
【发布时间】:2020-07-21 17:52:43
【问题描述】:

我在我的 Angular 应用程序中实现 SSR,而尝试运行 npm run build:ssr 它会引发以下错误。

我已经创建了自己的库,它是否捆绑在名为 @asfc/shared 的 dist 文件夹中

ERROR in projects/asfc-web/src/environments/environment.ts:1:39 - error TS2307: Cannot find module '@asfc/shared' or its corresponding type declarations.

1 import { PageNotFoundComponent } from '@asfc/shared';
                                        ~~~~~~~~~~~~~~
......

我已将 index.ts 添加到 files,但错误仍然没有消失

下面是我的tsconfig.server.json

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "removeComments": false,
    "strict": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitAny": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "experimentalDecorators": true,
    "pretty": true,
    "declaration": true,
    "outDir": "../../dist/server",
    "lib": ["es2016", "DOM"],
    "types": [
      "node"
    ],
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "files": [
    "server.ts",
    "../../dist/asfc-shared/public_api.d.ts"
  ],
  "angularCompilerOptions": {
    "entryModule": "../../projects/asfc-web/src/app/app.server.module.ts#AppServerModule"
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

下面是我的 Dist Libs 结构,

dist/asfc-shared
    ├── asfc-shared.d.ts
    ├── package.json
    ├── public_api.d.ts

请帮忙。

【问题讨论】:

    标签: angular typescript server-side-rendering angular-universal


    【解决方案1】:

    尝试在您的 tsconfig.json 中添加路径映射

      "compilerOptions": {
        
        ....
        
        "paths": {
          "@asfc/shared/*": [
            "../../dist/asfc-shared/*"
          ],
    
        }
      },
    

    您的配置中还排除了 dist 文件夹,但我猜这与包含您的 lib 的文件夹不同。

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 2021-07-29
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多