Gatsby: [v2][gatsby-transformer-yaml] YAMLException: expected a single document in the stream, but found more

Created on 10 Jul 2018  ยท  9Comments  ยท  Source: gatsbyjs/gatsby

Summary

Hello guys,

I am trying to use the plugin https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-yaml.

I finally correctly created the queries with graphql, but there's a problem when I try to build the application in a production environtment.

When I tried to build, the netlify command line printed this error:

YAMLException: expected a single document in the stream, but found more

The most curious thing is that, as i told you before, my queries to yaml files were working correctly, but I have just discovered that, deleting the .cache folder and running gatsby develop or gatbsy build, it returns the same error, but if i run this command a second time, everything works again.

How can I resolve this?

Thanks a lot for your help!

Relevant information

The folder structure for yml files is:

โ”œโ”€โ”€ _src
โ”‚   โ””โ”€โ”€ _data
โ”‚       โ””โ”€โ”€ _generic
โ”‚           โ”œโ”€โ”€ index.yml
โ”‚           โ”œโ”€โ”€ services.yml
โ”‚           โ””โ”€โ”€ ...

The query:

export const indexQuery = graphql`
  query allGeneric{
    allGenericYaml ( filter: {  published: { eq : true }, front_page: { eq : true} } ){
      edges {
        node {
          title
          path
        }
      }
    }
  }
`;

The yaml content file structure is something like this:

path: '/'
date: 2017-07-07T12:00:00Z
title: Inicio
published: true
front_page: false
cover: 'img/page/home.jpg'
navigation:
  - name: main
    order: 1
    level: 1
    hidden: false

Environment

  System:
    OS: Windows 10
    CPU: x64 Intel(R) Core(TM) i7-3930K CPU @ 3.20GHz
  Binaries:
    npm: 6.1.0 - C:\Users\harri\AppData\Roaming\npm\npm.CMD
  Browsers:
    Edge: 41.16299.492.0
  npmPackages:
    gatsby: next => 2.0.0-beta.19
    gatsby-plugin-react-helmet: next => 3.0.0-beta.3
    gatsby-source-filesystem: ^1.5.39 => 1.5.39
    gatsby-transformer-yaml: ^1.5.18 => 1.5.18

File contents

gatsby-config.js:

module.exports = {
  siteMetadata: {
    title: 'Gatsby Default Starter',
  },
  plugins: [
    'gatsby-plugin-react-helmet',
    `gatsby-transformer-yaml`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `./src/data/`,
      },
    },
  ],
}

package.json:

{
  "name": "gatsby-starter-default",
  "description": "Gatsby default starter",
  "version": "1.0.0",
  "author": "Kyle Mathews <[email protected]>",
  "dependencies": {
    "gatsby": "next",
    "gatsby-plugin-react-helmet": "next",
    "gatsby-source-filesystem": "^1.5.39",
    "gatsby-transformer-yaml": "^1.5.18",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-helmet": "^5.2.0"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --write 'src/**/*.js'",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "prettier": "^1.12.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/gatsbyjs/gatsby-starter-default"
  }
}
question or discussion

All 9 comments

Reading up on this error message (here and here), it sounds like a single Yaml file can contain multiple documents. Each new document is marked by ---. Do any of your Yaml files contain multiple documents?

Hi @m-allanson,

No, I don't have multiple documents inside the yaml file but neither I have the --- divider, is this divider necessary even if I have just one document inside the file with yml extension?

My yaml files only contains what I wrote before, in the section "The yaml content file structure is something like this".

Thanks for your help.

I have just tried modifiyng my .yml files for adding --- but it still not working.

I have been trying to get more information about the issue and I can see that the build is failing when the file .cache/redux-state.json doesn't exists.

Also, when I clear the cache folder and execute gatbsy develop, the error is shown by console, and after this, the console gets stuck with the message 'source and transform nodes'

I will to try go deeper.

Ok, this is embarrasing ๐Ÿ˜†

I have solved my problem making a bit of debugging, finally I found that I had another .yml files from other folder with the dividers --- and that's all.

Now the deploy is working fine.

Thanks a lot!

I'm glad you got it worked out! Maybe Gatsby should support Yaml files that contain multiple documents? I'm not really sure what impact that would have on the Yaml transformer.

I also met this problem when I run this code:

let obj = yaml.safeLoad(fs.readFileSync("F:/Demo1.yml", "utf8"));

and I do not have another .yml files in current folder, I don't know what to do now...

I'm glad you got it worked out! Maybe Gatsby should support Yaml files that contain multiple documents? I'm not really sure what impact that would have on the Yaml transformer.

if the --- notation is to indicate a document, then why not support several documents. For the application I am working on I need to support several documents in one file, it makes a lot of sense.

I'm glad you got it worked out! Maybe Gatsby should support Yaml files that contain multiple documents? I'm not really sure what impact that would have on the Yaml transformer.

if the --- notation is to indicate a document, then why not support several documents. For the application I am working on I need to support several documents in one file, it makes a lot of sense.

Agree. Loading from a .yaml file with a --- separator should return an array of yamls.

Thanks, the problem is solved

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TuckerWhitehouse picture TuckerWhitehouse  ยท  69Comments

cusspvz picture cusspvz  ยท  128Comments

dlindahl picture dlindahl  ยท  81Comments

freiksenet picture freiksenet  ยท  131Comments

jonathan-chin picture jonathan-chin  ยท  69Comments