最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Module x can only be default-imported using the allowSyntheticDefaultImports flag - Stack Overflow

programmeradmin5浏览0评论

I am updating from angular 17 to angular 19 using update guide /update-guide?v=17.0-19.0&l=3

After updating from v18 to v19 and changing

import * as moment from 'moment' to import moment from 'moment' and

import * as SockJS from 'sockjs-client' to import SockJS from 'sockjs-client'

i am getting errors from Moment.js and sockjs-client: Module can only be default-imported using the allowSyntheticDefaultImports flag

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "experimentalDecorators": true,
    "module": "es2020",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "useDefineForClassFields": false
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

I tried to use es2022 in 'module' and 'target', changed es2018 -> esnext in 'libs', cleared cache (intellij) but nothing helps. I am still facing the compile error. How to fix it?

Update: adding angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "dash-frontend": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "allowedCommonJsDependencies":[
                "@tweenjs/tween.js",
                "sockjs-client",
                "ng2-validation",
                "lodash",
                "babel-runtime",
                "@stomp/stompjs",
                "typescript-map",
                "angular7-csv-compat-16"
            ],
            "outputPath": {
              "base": "dist/ui"
            },
            "index": "src/index.html",
            "polyfills": ["zone.js"],
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/@fortawesome/fontawesome-free/css/fontawesome.css",
              "node_modules/@fortawesome/fontawesome-free/css/brands.css",
              "node_modules/@fortawesome/fontawesome-free/css/all.css",
              "node_modules/@fortawesome/fontawesome-free/css/regular.css",
              "node_modules/@fortawesome/fontawesome-free/css/solid.css",
              "node_modules/@fortawesome/fontawesome-free/css/svg-with-js.css",
              "node_modules/@fortawesome/fontawesome-free/css/v4-shims.css",
              "node_modules/material-design-icon-fonts/iconfont/material-icons.css",
              "node_modules/fullcalendar/dist/fullcalendar.css",
              "node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "node_modules/@ng-select/ng-select/themes/material.theme.css",
              "src/styles.scss"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/chart.js/dist/chart.umd.js",
              "node_modules/peity/jquery.peity.min.js",
              "node_modules/moment/min/moment.min.js",
              "node_modules/fullcalendar/dist/fullcalendar.min.js",
              "node_modules/jquery-fullscreen-plugin/jquery.fullscreen-min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
            "browser": "src/main.ts"
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "60mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "60kb"
                }
              ]
            },
            "development": {
              "extractLicenses": false,
              "sourceMap": true,
              "optimization": false,
              "namedChunks": true
            },
            "s3": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.s3.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "60mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "60kb"
                }
              ]
            },
            "staging":
            {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "60mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "60kb"
                }
              ]
            }

          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "buildTarget": "dash-frontend:build"
          },
          "configurations": {
            "production": {
              "buildTarget": "dash-frontend:build:production"
            },
            "development": {
              "buildTarget": "dash-frontend:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "buildTarget": "dash-frontend:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": ["zone.js"],
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "dash-frontend:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "dash-frontend:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "dash-frontend",
  "cli": {
    "analytics": false
  }
}

package.json

{
  "name": "dash-frontend",
  "version": "8.51.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build-prod": "node --max_old_space_size=16192 ./node_modules/@angular/cli/bin/ng build --prod"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^19.2.2",
    "@angular/cdk": "^19.2.3",
    "@angular/common": "^19.2.2",
    "@angular/compiler": "^19.2.2",
    "@angular/core": "^19.2.2",
    "@angular/forms": "^19.2.2",
    "@angular/material": "^19.2.3",
    "@angular/material-moment-adapter": "^19.2.3",
    "@angular/platform-browser": "^19.2.2",
    "@angular/platform-browser-dynamic": "^19.2.2",
    "@angular/router": "^19.2.2",
    "@fortawesome/fontawesome-free": "^6.1.1",
    "@ng-select/ng-select": "^14.2.4",
    "@ngx-translate/core": "^14.0.0",
    "@ngx-translate/http-loader": "^8.0.0",
    "@pascalhonegger/ng-datatable": "^19.0.0",
    "@stomp/stompjs": "^5.4.4",
    "@types/sockjs-client": "^1.5.4",
    "angular7-csv-compat-16": "^0.2.13",
    "app-root-path": "^3.0.0",
    "bootstrap": "^4.5.3",
    "chart.js": "^4.4.0",
    "fullcalendar": "^3.10.2",
    "jquery": "^3.5.1",
    "jquery-fullscreen-plugin": "^1.1.5",
    "material-design-icon-fonts": "^3.0.1",
    "moment": "^2.30.1",
    "ng2-charts": "^5.0.3",
    "ngx-file-drop": "^15.0.0",
    "ngx-mat-select-search": "^8.0.0",
    "ngx-ui-loader": "^13.0.0",
    "path": "^0.12.7",
    "peity": "^3.3.0",
    "popper.js": "^1.16.1",
    "postcss-loader": "^8.1.1",
    "rxjs": "^7.8.1",
    "rxjs-compat": "^6.6.7",
    "sass": "^1.45.0",
    "sass-loader": "^10.1.1",
    "sockjs-client": "^1.6.1",
    "tile": "0.0.1",
    "ts-md5": "^1.3.1",
    "tslib": "^2.0.0",
    "typescript": "5.6.3",
    "typescript-map": "0.0.7",
    "zone.js": "~0.15.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^19.2.3",
    "@angular/cli": "^19.2.3",
    "@angular/compiler-cli": "^19.2.2",
    "@angular/language-service": "^19.2.2",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^20.5.1",
    "codelyzer": "^0.0.28",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "^6.3.20",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "^7.0.0",
    "ts-node": "~7.0.0",
    "tslint": "~6.1.0"
  }
}
发布评论

评论列表(0)

  1. 暂无评论