Skip to content

Can't resolve '<type>/_index.json' #208

Closed

Description

@jpedroschmitz

Given the following config on a Next.js project:

export const Post = defineDocumentType(() => ({
  name: 'Post',
  filePathPattern: `**/*.md`,
  fields: {
    title: {
      type: 'string',
      description: 'The title of the post',
      required: true,
    },
    date: {
      type: 'date',
      description: 'The date of the post',
      required: true,
    },
  },
  computedFields: {
    url: {
      type: 'string',
      resolve: (post) => `/posts/${post._raw.flattenedPath}`,
    },
  },
}))

export default makeSource({
  contentDirPath: 'posts',
  documentTypes: [Post],
})

If you don't create a Markdown file on the folder, the _index.json file won't be created, which will generate the following error when building:

Failed to compile.

.contentlayer/generated/index.mjs:10:0
Module not found: Can't resolve './Post/_index.json'

Although it's not a big issue, Contentlayer should generate the file with an empty array even if there's no content, so there are no errors when building.


CodeSandbox example: https://codesandbox.io/s/ecstatic-goldstine-s9oryb

If you start a dev server, no errors since Contentlayer will use the _index.mjs file, which looks like this:

// NOTE This file is auto-generated by Contentlayer

export const allPosts = []

But once you build, you'll see the error.

Activity

  1. added
    bug
    on May 9, 2022
  2. added this to the 0.2.5 milestone on May 9, 2022
  3. schickling commented on May 11, 2022

    @schickling
    Collaborator

    This should be addressed with the 0.2.5 release. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions