1

nestjs脚手架生成库出错 - 搞搞震

 3 years ago
source link: https://www.wujingquan.com/posts/a98b3509.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
nestjs脚手架生成库出错 - 搞搞震

最近把nestjs写的程序调整了一下,项目结构从Standard Mode该为Monorepo Mode,且把一些共用的模块提取出来,做成库。使用Nest CLI脚手架,执行了nest g lib db生成一个库,但是报错了:

Error: Invalid JSON character: "\"" at 13:6.
    at _token (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:81:19)
    at _readProperty (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:490:5)
    at _readObject (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:524:30)
    at _readValue (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:674:22)
    at _readProperty (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:491:19)
    at _readObject (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:515:26)
    at _readValue (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:674:22)
    at parseJsonAst (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:721:17)
    at Object.parseJson (/root/recovery-backend/node_modules/@angular-devkit/core/src/json/parser.js:750:24)
    at updateJsonFile (/root/recovery-backend/node_modules/@nestjs/schematics/dist/lib/library/library.factory.js:110:29)

Failed to execute command: "/root/recovery-backend/node_modules/.bin/schematics" @nestjs/schematics:library --name=db --no-dry-run --collection="@nestjs/schematics" --language="ts" --sourceRoot="apps/server/src" --spec

虽然从错误信息得知是脚手架在解析JSON的时候出错了,但是却不知道具体的原因。我们先从报错的栈中/root/recovery-backend/node_modules/@nestjs/schematics/dist/lib/library/library.factory.js文件updateJsonFile函数中打印一下,看看sourceText是什么。

function updateJsonFile(host, path, callback) {
    const source = host.read(path);
    if (source) {
        const sourceText = source.toString('utf-8');
        console.log(sourceText) // 打印信息
        const json = core_1.parseJson(sourceText);
        callback(json);
        host.overwrite(path, JSON.stringify(json, null, 2));
    }
    return host;
}
{
  "collection": "@nestjs/schematics",
  "sourceRoot": "apps/server/src",
  "monorepo": true,
  "root": "apps/server",
  "compilerOptions": {
    "webpack": false,
    "tsConfigPath": "apps/server/tsconfig.app.json"
  },
  "projects": {
    "server": {
      "type": "application",
      "root": "apps/server",
      "entryFile": "main",
      "sourceRoot": "apps/server/src",
      "compilerOptions": {
        "webpack": false,
        "tsConfigPath": "apps/server/tsconfig.app.json"
      }
    },
    "admin": {
      "type": "application",
      "root": "apps/admin",
      "entryFile": "main",
      "sourceRoot": "apps/admin/src",
      "compilerOptions": {
        "webpack": false,
        "tsConfigPath": "apps/admin/tsconfig.app.json"
      }
    }
  }
}
{
  "name": "recovery-backend",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "license": "MIT",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint '{src,apps,libs,test}/**/*.ts' --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./apps/recovery-backend/test/jest-e2e.json"
  },
  "dependencies": {
    "@alicloud/pop-core": "^1.7.9",
    "@nestjs/common": "^7.0.5",
    "@nestjs/config": "^0.4.0",
    "@nestjs/core": "^7.0.5",
    "@nestjs/jwt": "^7.0.0",
    "@nestjs/passport": "^7.0.0",
    "@nestjs/platform-express": "^7.0.5",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.11.1",
    "co-wechat-oauth": "^2.0.1",
    "crypto-js": "^4.0.0",
    "mikro-orm": "^3.5.2",
    "moment": "^2.24.0",
    "mysql2": "^2.1.0",
    "nestjs-mikro-orm": "^1.0.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "qcloudsms_js": "^0.1.1",
    "querystring": "^0.2.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.0",
    "rxjs": "^6.5.4",
    "wechat-jssdk": "^5.0.4"
  },
  "devDependencies": {
    "@nestjs/cli": "^6.13.2",
    "@nestjs/schematics": "^6.8.1",
    "@nestjs/testing": "^6.11.11",
    "@types/express": "^4.17.3",
    "@types/jest": "25.1.1",
    "@types/node": "^13.9.4",
    "@types/passport-jwt": "^3.0.3",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "^2.25.0",
    "@typescript-eslint/parser": "^2.25.0",
    "eslint": "^6.7.2",
    "eslint-config-prettier": "^6.10.1",
    "eslint-plugin-import": "^2.19.1",
    "jest": "^24.9.0",
    "prettier": "^1.18.2",
    "supertest": "^4.0.2",
    "ts-jest": "25.1.0",
    "ts-loader": "^6.2.2",
    "ts-node": "^8.8.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": ".",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "./coverage",
    "testEnvironment": "node",
    "roots": [
      "<rootDir>/apps/"
    ]
  },
  "mikro-orm": {
    "useTsNode": true,
    "configPaths": [
      "./apps/server/src/mikro-orm.config.ts",
      "./dist/apps/server/mikro-orm.config.js"
    ]
  }
}

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "esModuleInterop": true,
    // "paths": {}
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

我们可以看到,打印出来的信息最下面的部分:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "esModuleInterop": true,
    // "paths": {}
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

其中的这一行被注释了

"paths": {}
// "paths": {}

被注释掉了,我们知道,标准严格的JSON格式是不能有注释的。把该行改正问题即可解决。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK