APIDevTools / swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser/validator
https://apitools.dev/swagger-parser
MIT License
1.1k stars 155 forks source link

process is not defined #133

Closed chaos1ee closed 4 years ago

chaos1ee commented 4 years ago

Hi, I'm using swagger-parser in project created by angular-cli, and encountered the following error.

Uncaught ReferenceError: process is not defined
    at Object../node_modules/json-schema-ref-parser/lib/util/url.js (url.js:3)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/json-schema-ref-parser/lib/resolvers/file.js (file.js:4)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/json-schema-ref-parser/lib/options.js (options.js:8)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/json-schema-ref-parser/lib/normalize-args.js (normalize-args.js:3)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/swagger-parser/lib/index.js (index.js:5)
    at __webpack_require__ (bootstrap:79)

I just wonder if this package supported in browser.

chaos1ee commented 4 years ago

I have tried swagger-parser in vue-cli project, and it running successfully. I think this issue can be due to some configs in angular-cli.

chaos1ee commented 4 years ago

This problem caused by node polyfils had been removed since angular-cli 6.x.

JamesMessinger commented 4 years ago

It looks like you're using Webpack to bundle Swagger Parser for running in a browser. So you'll need to configure Webpack and/or Babel and whatever polyfills are needed for your target environment. See the browser support topic in the ReadMe for details.

chaos1ee commented 4 years ago

Add the flowing codes in "polyfills.ts" can resolve this problem. I have tested, it works.

import * as process from 'process';

(window as any).global = window;
(window as any).process = process;

// @ts-ignore
(window as any).Buffer = window.Buffer || require('buffer').Buffer;