NextJS Netlify CMS TypeError: data.tags.forEach is not a function

forEach is not a function, and i’m not front-end dev enough to know how to fix this so any help is appreciated!

  • Local NextJS build is ok
  • Netlify build and deploy is ok
  • Netlify CMS fails build on Save Draft

Netlify deloy log failure message

12:03:32 PM: > Build error occurred
12:03:32 PM: TypeError: data.tags.forEach is not a function
12:03:32 PM:     at /opt/build/repo/.next/server/pages/tags/[tag].js:39:23
12:03:32 PM:     at Array.forEach (<anonymous>)
12:03:32 PM:     at getAllTags (/opt/build/repo/.next/server/pages/tags/[tag].js:35:11)
12:03:32 PM:     at async getStaticPaths (/opt/build/repo/.next/server/pages/tags/[tag].js:95:18)
12:03:32 PM:     at async buildStaticPaths (/opt/build/repo/node_modules/next/dist/build/utils.js:490:31)
12:03:32 PM:     at async /opt/build/repo/node_modules/next/dist/build/utils.js:632:119
12:03:32 PM:     at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:79:20) {
12:03:32 PM:   type: 'TypeError'
12:03:32 PM: }
12:03:32 PM: ​
12:03:32 PM: build.command failed                                        
12:03:32 PM: ───────────────
12:03:32 PM: ​
12:03:32 PM:   Error message
12:03:32 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
12:03:32 PM: ​
12:03:32 PM:   Error location
12:03:32 PM:   In Build command from Netlify app:
12:03:32 PM:   npm run build
12:03:32 PM: ​
12:03:32 PM:   Resolved config
12:03:32 PM:   build:
12:03:32 PM:     command: npm run build
12:03:32 PM:     commandOrigin: ui
12:03:32 PM:     environment:
12:03:32 PM:       - NEXT_PRIVATE_TARGET
12:03:32 PM:     publish: /opt/build/repo/.next
12:03:32 PM:     publishOrigin: ui
12:03:32 PM:   plugins:
12:03:32 PM:     - inputs: {}
12:03:32 PM:       origin: ui
12:03:32 PM:       package: '@netlify/plugin-nextjs'
12:03:33 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:03:33 PM: Failing build: Failed to build site
12:03:33 PM: Finished processing build request in 37.363s

This is my tags.js source

import fs from 'fs'
import matter from 'gray-matter'
import path from 'path'
import { getFiles } from './mdx'
import kebabCase from './utils/kebabCase'

const root = process.cwd()

export async function getAllTags(type) {
  const files = await getFiles(type)

  let tagCount = {}
  // Iterate through each post, putting all found tags into `tags`
  files.forEach((file) => {
    const source = fs.readFileSync(path.join(root, 'data', type, file), 'utf8')
    const { data } = matter(source)
    if (data.tags && data.draft !== true) {
      data.tags.forEach((tag) => {
        const formattedTag = kebabCase(tag)
        if (formattedTag in tagCount) {
          tagCount[formattedTag] += 1
        } else {
          tagCount[formattedTag] = 1
        }
      })
    }
  })

  return tagCount
}

@wristtattoo The error indicates that data.tags isn’t an Array like the code presumes that it is.

You should confirm that data.tags exists, and what it actually is.

1 Like