简体   繁体   English

为什么我收到以下错误“数据路径“/错误”必须是字符串。 在 angular 中建立我的图书馆?

[英]Why i am getting the following error 'Data path "/error" must be string.' on building my library in angular?

When i am trying to build my angular-library i am getting the following error: Schema validation failed with the following errors: Data path "/error" must be string.当我尝试构建我的角度库时,出现以下错误: Schema validation failed with the following errors: Data path "/error" must be string. ; ;

I am getting this error until i migrated from angular 12 to angular 13.在我从 angular 12 迁移到 angular 13 之前,我收到了这个错误。

This is my declaration in angular.json in my 'Main-application':这是我在“主应用程序”中的angular.json中的声明:

"projectname": {
  "projectType": "library",
  "root": "projects/projectname",
  "sourceRoot": "projects/projectname/src",
  "prefix": "pn",
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:ng-packagr",
      "options": {
        "tsConfig": "projects/projectname/tsconfig.lib.json",
        "project": "projects/projectname/ng-package.json",
      },
      "configurations": {
        "production": {
          "tsConfig": "projects/projectname/tsconfig.lib.prod.json"
        }
      }
    },
    "test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "projects/projectname/src/test.ts",
        "tsConfig": "projects/projectname/tsconfig.spec.json",
        "karmaConfig": "projects/projectname/karma.conf.js"
      }
    }
  }
}

This is the package.json of my 'main-application':这是我的“主应用程序”的 package.json:

  "name": "mainApplication",
  "version": "2.0.0",
  "private": true,
  "devDependencies": {
    "@angular-builders/custom-webpack": "^13.1.0",
    "@angular-devkit/build-angular": "~13.3.0",
    "@angular-eslint/builder": "13.1.0",
    "@angular-eslint/eslint-plugin": "13.1.0",
    "@angular-eslint/eslint-plugin-template": "13.1.0",
    "@angular-eslint/schematics": "^13.1.0",
    "@angular-eslint/template-parser": "13.1.0",
    "@angular/cli": "~13.3.0",
    "@angular/compiler-cli": "~13.3.0",
    "@angular/language-service": "~13.3.0",
    "@biesbjerg/ngx-translate-extract": "^7.0.4",
    "@biesbjerg/ngx-translate-extract-marker": "^1.0.0",
    "@types/file-saver": "^2.0.5",
    "@types/hashids": "^2.0.1",
    "@types/moment-timezone": "^0.5.12",
    "@types/uuid": "^8.3.4",
    "@typescript-eslint/eslint-plugin": "5.15.0",
    "@typescript-eslint/parser": "5.15.0",
    "autoprefixer": "^10.4.4",
    "eslint": "^8.11.0",
    "eslint-plugin-import": "2.25.4",
    "eslint-plugin-jsdoc": "38.0.4",
    "eslint-plugin-prefer-arrow": "1.2.3",
    "hashids": "^2.2.10",
    "html-webpack-plugin": "^5.5.0",
    "invariant": "^2.2.4",
    "postcss": "^8.4.12",
    "typescript": "~4.6.2",
    "webpack-bundle-analyzer": "^4.5.0"
  },
  "engines": {
    "node": ">=0.10.0"
  },
  "scripts": {
    "build": "ng build",
    "build:dev": "ng build",
    "build:prod": "ng build --configuration production",
    "build:stats": "ng build --stats-json",
    "build:stats-prod": "ng build --stats-json  --configuration production",
    "test": "karma start test/karma.conf.js",
    "serve": "ng serve --hmr --ssl --disable-host-check",
    "analyze": "webpack-bundle-analyzer dist/payweb/de/stats.json",
    "i18n": "ng xi18n --ivy --output-path app/i18n/ --format xliff2",
    "i18n:extract": "find ./app/components/ -name 'i18n' -type d -exec ngx-translate-extract -i {}/../ -o {}/de.json --key-as-default-value -f namespaced-json --fi '  ' \\;",
    "i18n:init": "find ./app/components/ -name 'i18n' -type d -exec ngx-translate-extract -i {}/../ -o {}/template.json  --replace --key-as-default-value -f namespaced-json --fi '  ' \\;"
  },
  "dependencies": {
    "@angular/animations": "^13.3.0",
    "@angular/cdk": "^13.3.0",
    "@angular/common": "^13.3.0",
    "@angular/compiler": "^13.3.0",
    "@angular/core": "^13.3.0",
    "@angular/flex-layout": "^13.0.0-beta.38",
    "@angular/forms": "^13.3.0",
    "@angular/localize": "^13.3.0",
    "@angular/material": "~13.3.0",
    "@angular/material-moment-adapter": "^13.3.0",
    "@angular/platform-browser": "^13.3.0",
    "@angular/platform-browser-dynamic": "^13.3.0",
    "@angular/router": "^13.3.0",
    "@ngrx/component": "^13.0.2",
    "@ngrx/effects": "^13.0.2",
    "@ngrx/entity": "^13.0.2",
    "@ngrx/router-store": "^13.0.2",
    "@ngrx/schematics": "^13.0.2",
    "@ngrx/store": "^13.0.2",
    "@ngrx/store-devtools": "^13.0.2",
    "@ngx-translate/core": "^14.0.0",
    "compare-versions": "^4.1.3",
    "file-saver": "^2.0.5",
    "messageformat": "^2.3.0",
    "moment": "^2.29.1",
    "moment-timezone": "^0.5.34",
    "ng-packagr": "^13.3.0",
    "ngx-file-helpers": "^7.0.0",
    "ngx-filesaver": "^13.0.0",
    "ngx-moment": "^6.0.2",
    "ngx-translate-messageformat-compiler": "^5.0.1",
    "rxjs": "^7.5.5",
    "tslib": "^2.3.1",
    "uuid": "^8.3.2",
    "zone.js": "~0.11.5"
  }

This is my package.json in my library:这是我图书馆中的 package.json:

{
  "name": "projectname",
  "version": "0.0.2",
  "peerDependencies": {
    "@angular/common": "^13.3.0",
    "@angular/core": "^13.3.0",
    "tslib": "^2.3.1"
  }
}

If you need further information let me know.如果您需要更多信息,请告诉我。

I had the same error and found the solution for my context.我遇到了同样的错误,并找到了适合我的上下文的解决方案。

Context语境

I have a mono-repo with applications and libraries.我有一个包含应用程序和库的单一存储库。 Libraries are split into sub-libraries.库分为子库。

▾ libraries/
  ▾ my-library/
    ▾ src/
      ▾ lib/
        ▾ sub-library-1/
            sub-library-1.module.ts
            package.json
        ▾ sub-library-2/
            sub-library-2.module.ts
            package.json
        ▾ sub-library-3/
            sub-library-3.module.ts
            package.json
        ▾ sub-library-4/
            sub-library-4.module.ts
            package.json
        index.ts
      ng-package.json
      package.json

Outside the library the inner library structure is hidden so if an application consumes a member of sub-library-3 it just needs to:在库之外,内部库结构是隐藏的,因此如果应用程序使用sub-library-3的成员,它只需要:

import { Member } from 'my-library'

But if an inner library consumes another inner library, eg:但是如果一个内部库消耗另一个内部库,例如:

Member of sub-library-2 consumes a Member of sub-library-3 , the import should be absolute: Member of sub-library-2Member of sub-library-3的成员,导入应该是绝对的:

import { Member of sub-library-3 } from 'my-library/src/lib/sub-library-3';

Problem问题

I got this error because I was wrongly using a relative path:我收到此错误是因为我错误地使用了相对路径:

import { Member of sub-library-3 } from '../sub-library-3';

Solution解决方案

Changing it to use an absolute path, fixed the problem将其更改为使用绝对路径,解决了问题

import { Member of sub-library-3 } from 'my-library/src/lib/sub-library-3';

This might not be your exact case as the error could occur due to a wrong import in very different contexts but in my experience, most of these kinds of problems have been caused by a wrong import path.这可能不是您的确切情况,因为错误可能是由于在非常不同的上下文中导入错误而发生的,但根据我的经验,大多数此类问题都是由错误的导入路径引起的。

Hope this helps希望这可以帮助

In case someone are still scratching your head after reading the previous answers.以防有人在阅读之前的答案后仍然摸不着头脑。

I have a similar issue when upgrading to angular13.升级到 angular13 时我遇到了类似的问题。 Several libraries, the libraries are located under projects folder.几个库,这些库位于projects文件夹下。 Got the same error when compiling a lib2 that depends on lib1.编译依赖于 lib1 的 lib2 时出现相同的错误。 Lib1 passed well, but lib2 didn't. Lib1 顺利通过,但 lib2 没有。

FYI, Using projects/... , ie the absolute path, didn't fix my problem.仅供参考,使用projects/... ,即绝对路径,并没有解决我的问题。

I fixed it by publishing the lib1, then added the lib in package.json, then in typescript, import the lib as all other libs such as:我通过发布 lib1 来修复它,然后将 lib 添加到 package.json 中,然后在 typescript 中,将 lib 作为所有其他库导入,例如:

In package.json dependencies section:在 package.json dependencies项部分:

"lib1": "latest",

in my comp.ts:在我的 comp.ts 中:

import { MyItem } from 'lib1';

暂无
暂无

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

相关问题 Angular 13 迁移:架构验证失败并出现以下错误:数据路径“/错误”必须是字符串 - Angular 13 migration : Schema validation failed with the following errors: Data path "/error" must be string Angular 在我正在构建的库中使用自己的库时构建库错误 - Angular building library error when using own library in library i am building 使用 npm run build:prerender 构建我的 angular 通用应用程序时出现一些错误 - I am getting some error when building my angular universal app with npm run build:prerender 为什么我收到 InvalidValueError: in property origin: must set one of location, placeId or query error in angular? - Why am i getting InvalidValueError: in property origin: must set one of location, placeId or query error in angular? angular-cli路径必须是字符串。 收到未定义 - angular-cli Path must be a string. Received undefined Angular ngFor 绑定错误。 为什么我收到错误? - Angular ngFor binding error. Why am I getting an error? 为什么在我的 Angular 代码中出现此错误? 错误:InvalidPipeArgument - Why in my Angular code I am obtaining this error? Error: InvalidPipeArgument 为什么将我的 Angular 项目部署到 Heroku 时出现 ng not found 错误? - Why am I getting an ng not found error when deploying my Angular project to Heroku? Angular 6错误:我从api获取的数据是字符串格式,如何解析? - Angular 6 error: the data which I am getting from api is in the string format how to parse? 在实现角度为6的条带时出现错误错误:mat-form-field必须包含MatFormFieldControl - While implementing stripe with angular 6 I am getting ERROR Error: mat-form-field must contain a MatFormFieldControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM