Chloe
Chloe

Reputation: 26294

Swagger cannot parse YAML: "SyntaxError: Unexpected token :"

I tried to follow this guide: https://blog.cloudboost.io/adding-swagger-to-existing-node-js-project-92a6624b855b. I changed this line to use YAML:

swaggerDocument = require('./swagger.yml');

The example from the Swagger editor does not work. http://editor.swagger.io/

tags:
  - name: "pet"
    description: "Everything about your Pets"

Gave this error:

  - name: "pet"                
        ^                      

SyntaxError: Unexpected token :

I tried to change the indentation but it didn't help. I looked at the specification but it says that is OK: https://yaml.org/spec/1.2/spec.html#id2761803

Upvotes: 0

Views: 3097

Answers (1)

Chloe
Chloe

Reputation: 26294

I had to change it to

var swaggerUi = require('swagger-ui-express')
var fs = require('fs')
var jsyaml = require('js-yaml');
var spec = fs.readFileSync('swagger.yml', 'utf8');
var swaggerDocument = jsyaml.safeLoad(spec);

And move app/swagger.yml to the project root directory. fs.readFileSync() cannot understand ./swagger.yml the way require() does.

Upvotes: 2

Related Questions