此模块使用“export =”声明,并且只能在使用“allowSyntheticDefaultImports”标志时与默认导入一起使用

r08*_*r08 2 lodash angular-cli angular

我在执行时遇到以下错误npm publish,但构建工作正常。

下面是运行发布包的命令。

npm run ng-packagr -p projects/core/package.json && cd dist/@app/formgenerator-core && npm publish

包.json

{
  "name": "@app/formgenerator-core",
  "version": "0.0.4",
  "description": "Angular JSON schema form generator core",
  "author": "devteam",
  "keywords": [
    "angular json schema form generator"
  ],
  "contributors": [
    "devteam@gmail.com"
  ],
  "private": false,
  "license": "UNLICENSED",
  "ngPackage": {
    "$schema": "../../node_modules/ng-packagr/package.schema.json",
    "dest": "../../dist/@app/formgenerator-core",
    "lib": {
      "entryFile": "src/public_api.ts",
      "umdModuleIds": {
        "lodash/isEqual": "lodash-es",
        "lodash/cloneDeep": "lodash-es",
        "lodash/filter": "lodash-es",
        "lodash/map": "lodash-es",
        "lodash/uniqueId": "lodash-es",
        "ajv": "ajv",
        "ajv/lib/refs/json-schema-draft-06.json": "jsonDraft6",
        "hot-formula-parser": "hot-formula-parser",
        "@app/app-virtual-keypad": "@app/app-virtual-keypad"
      }
    },
    "whitelistedNonPeerDependencies": [
      "lodash-es",
      "ajv",
      "hot-formula-parser",
      "@app/app-virtual-keypad"
    ]
  },
  "dependencies": {
    "@app/app-virtual-keypad": "0.1.2",
    "ajv": "6.12.3",
    "hot-formula-parser": "3.0.2",
    "lodash": "^4.17.20",
    "lodash-es": "^4.17.15"
  },
  "peerDependencies": {
    "@angular/common": ">=6.0.0",
    "@angular/core": ">=6.0.0",
    "@angular/forms": ">=6.0.0",
    "@angular/platform-browser": ">=6.0.0",
    "rxjs": ">=6.0.0"
  },
  "devDependencies": {
    "@types/lodash-es": "4.17.3"
  },
  "main": "karma.conf.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  }
}
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "@app/formgenerator-core": [
        "dist/@app/formgenerator-core"
      ],
      "@app/formgenerator-core/*": [
        "dist/@app/formgenerator-core/*"
      ]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
Run Code Online (Sandbox Code Playgroud)

控制台错误:

Compiling TypeScript sources through ngc
ERROR: projects/core/src/lib/shared/json.validators.ts:1:8 - error TS1259: Module '"D:/app2020/septmber/formgenerator/projects/core/node_modules/@types/lodash/isEqual"' can only be default-imported using the 'allowSyntheticDefaultImports' flag

1 import isEqual from 'lodash/isEqual';
         ~~~~~~~

  projects/core/node_modules/@types/lodash/isEqual.d.ts:2:1
    2 export = isEqual;
      ~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
Run Code Online (Sandbox Code Playgroud)

在我的项目中是否缺少任何配置或额外需要添加。

请任何帮助是非常appriciated。

谢谢。

Sun*_*mar 5

你娘家添加"allowSyntheticDefaultImports": true在您的<projectroot>/tsconfig.json文件,如下

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "exclude": ["node_modules"],
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "allowSyntheticDefaultImports": true <------ add this
  }
  
}
Run Code Online (Sandbox Code Playgroud)


OrY*_*rYo 5

也尝试添加(我帮助了我):“esModuleInterop”:true,在“compilerOptions”中

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
     "esModuleInterop": true <------ add here
    "lib": [
      "es2018",
      "dom"
    ]
  },

  
}
Run Code Online (Sandbox Code Playgroud)