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 as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.45k stars 235 forks source link

SyntaxError: Unexpected token 'export' when running spectral lint #2656

Closed heller-tobias closed 1 month ago

heller-tobias commented 1 month ago

Describe the bug When running the spectral-cli in version 6.11.1 I get the following error SyntaxError: Unexpected token 'export' .

To Reproduce

  1. Run this CLI command 'spectral lint'
  2. See error

/usr/local/lib/node_modules/@stoplight/spectral-cli/node_modules/@stoplight/json/index.js:1 export * from './bundle'; ^^^^^^ SyntaxError: Unexpected token 'export' at internalCompileFunction (node:internal/vm:128:18) at wrapSafe (node:internal/modules/cjs/loader:1279:20) at Module._compile (node:internal/modules/cjs/loader:1331:27) at Module._extensions..js (node:internal/modules/cjs/loader:1426:10) at Module.load (node:internal/modules/cjs/loader:1205:32) at Module._load (node:internal/modules/cjs/loader:1021:12) at Module.require (node:internal/modules/cjs/loader:1230:19) at require (node:internal/modules/helpers:179:18) at Object. (/usr/local/lib/node_modules/@stoplight/spectral-cli/node_modules/@stoplight/spectral-runtime/dist/utils/decodeSegmentFragment.js:4:16) at Module._compile (node:internal/modules/cjs/loader:1368:14) Node.js v21.7.3

Expected behavior We should be able to run spectral without an issue.

Screenshots

Environment (remove any that are not applicable):

dimleevik commented 1 month ago

Spectral looks at latest version of https://github.com/stoplightio/json library and they've released new version of their package. As someone suggested under this issue https://github.com/stoplightio/json/issues/133 - spectral must be pinned to stable version, not the latest. BTW there is temporal workaround mentioned

rajki commented 1 month ago

Also impacted by the issue. For as of yet unknown reasons, the suggested workaround doesn't help.

RaffaeleCanale commented 1 month ago

We are also impacted by this. We temporarily worked around this issue with a package-lock.json to unblock ourselves for now:

{
  "dependencies": {
    "@stoplight/json": "^3.20.3",
    "@stoplight/spectral-cli": "^6.5.0"
  },
  "overrides": {
    "@stoplight/spectral-cli": {
      "@stoplight/json": "3.20.3"
    },
    "@stoplight/spectral-parsers": {
      "@stoplight/json": "3.20.3"
    }
  }
}

This worked for us, but your mileage may vary.

miriamgreis commented 1 month ago

Impacted as well. We're only using the cli. Would appreciate if it gets fixed soon.

arghideutis commented 1 month ago

Impacted too :)

DashKenny commented 1 month ago

Seeing this on 6.6.0 - traceback from Docker container:

root@3ca4380472f8:/app# spectral lint openapi.json --fail-severity=warn
/usr/local/lib/node_modules/@stoplight/spectral-cli/node_modules/@stoplight/spectral-core/node_modules/@stoplight/json/index.js:1
export * from './bundle';
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at Module._compile (node:internal/modules/cjs/loader:1320:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/@stoplight/spectral-cli/node_modules/@stoplight/spectral-core/dist/spectral.js:5:16)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)

Node.js v18.19.0
root@3ca4380472f8:/app# npm list -g
/usr/local/lib
├── @stoplight/spectral-cli@6.6.0
└── speccy@0.11.0
miriamgreis commented 1 month ago

Seeing this on 6.6.0 - traceback from Docker container:

root@3ca4380472f8:/app# spectral lint openapi.json --fail-severity=warn
/usr/local/lib/node_modules/@stoplight/spectral-cli/node_modules/@stoplight/spectral-core/node_modules/@stoplight/json/index.js:1
export * from './bundle';
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at Module._compile (node:internal/modules/cjs/loader:1320:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/@stoplight/spectral-cli/node_modules/@stoplight/spectral-core/dist/spectral.js:5:16)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)

Node.js v18.19.0
root@3ca4380472f8:/app# npm list -g
/usr/local/lib
├── @stoplight/spectral-cli@6.6.0
└── speccy@0.11.0

As described above, an updated dependency is causing the issue. The dependency is not pinned in Spectral but set to latest and got updated. So all Spectral versions are affected.

todurnatesh commented 1 month ago

As someone mentioned it here, this works - https://github.com/stoplightio/json/issues/133#issuecomment-2236318961

mnaumanali94 commented 1 month ago

We are currently working on fixing this!

ofer-h commented 1 month ago

We encountered a similar issue in one of our libraries and managed to resolve it when working with npx in CI by replacing package-lock.json with npm-shrinkwrap.json. We missed the fact that package-lock.json is not published to npm, whereas npm-shrinkwrap.json is. This change ensured the correct versions of dependencies were used consistently.

For more details, you can refer to the documentation: npm shrinkwrap.

mnaumanali94 commented 1 month ago

This should be fixed with this release: https://github.com/stoplightio/json/releases/tag/v3.21.3

I'll keep this issue open for a bit before closing. Let us know if y'all are still seeing issues.