stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.51k stars 240 forks source link

TypeError: Cannot destructure property 'Spectral' of 'spectral_core_1.default' as it is undefined. #2421

Closed AmitDhingra134 closed 1 year ago

AmitDhingra134 commented 1 year ago

When I am trying to integrate spiral stoplightio in a javascript application in VS code then its showing me below error on line number 9:

TypeError: Cannot destructure property 'Spectral' of 'spectral_core_1.default' as it is undefined.

Code :

  1. import * as fs from "node:fs";
  2. import { fileURLToPath } from "node:url";
  3. import * as path from "node:path";
  4. import { join } from "path";
  5. import { bundleAndLoadRuleset } from "@stoplight/spectral-ruleset-bundler/with-loader";
  6. //const { bundleAndLoadRuleset } = require("@stoplight/spectral-ruleset-bundler/with-loader");
  7. import Parsers from "@stoplight/spectral-parsers"; // make sure to install the package if you intend to use default parsers!
  8. import spectralCore from "@stoplight/spectral-core";
  9. const { Spectral, Document } = spectralCore;
  10. import spectralRuntime from "@stoplight/spectral-runtime";
  11. const { fetch } = spectralRuntime;
  12. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  13. //file:///C:/path/
  14. const myDocument = new Document(
  15. // load an API specification file from your project's root directory. You can use the openapi.yaml example from here: https://github.com/stoplightio/Public-APIs/blob/master/reference/plaid/openapi.yaml
  16. fs.readFileSync(join(__dirname, ".openapi.yaml"), "utf-8").trim(),
  17. Parsers.Yaml,
  18. ".openapi.yaml",
  19. );
  20. const spectral = new Spectral();
  21. // load a ruleset file from your project's root directory.
  22. const rulesetFilepath = path.join(__dirname, ".spectral.yaml");
  23. console.log(rulesetFilepath);
  24. spectral.setRuleset(bundleAndLoadRuleset(rulesetFilepath, { fs, fetch }));
  25. console.log(myDocument);
  26. spectral.run(myDocument).then(console.log);

Please help

Thanks AD