contentlayer: Build dependencies failed at import and can't resolve 'path'

I’m new here. Thanks for this awesome project! So, currently I’m getting this issue, I’m invested time to solve, but without success. Any help is welcome.

[webpack.cache.PackFileCacheStrategy/webpack.FileSystemInfo] Parsing of /node_modules/@contentlayer/core/dist/getConfig/index.js for build dependencies failed at 'import(`file://${modulePath}?x=${new Date()}`)'.
Build dependencies behind this expression are ignored and might cause incorrect cache invalidation.
[webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'path' in '/node_modules/@contentlayer/core/dist'
while resolving 'path' in /node_modules/@contentlayer/core/dist as file
 at resolve esm file path
 at file dependencies /node_modules/@contentlayer/core/dist/mdx.js
 at file /node_modules/@contentlayer/core/dist/mdx.js
 at resolve esm file ./mdx.js
 at file dependencies /node_modules/@contentlayer/core/dist/index.js
 at file /node_modules/@contentlayer/core/dist/index.js
 at resolve esm file @contentlayer/core
 at file dependencies /node_modules/next-contentlayer/dist/plugin.js
 at file /node_modules/next-contentlayer/dist/plugin.js
 at resolve esm file ./plugin.js
 at file dependencies /node_modules/next-contentlayer/dist/index.js
 at file /node_modules/next-contentlayer/dist/index.js
 at resolve esm file next-contentlayer
 at file dependencies /apps/web/next.config.mjs
 at file /apps/web/next.config.mjs
 at resolve commonjs /apps/web/next.config.mjs
[webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'path' in '/node_modules/@contentlayer/core/dist'
while resolving 'path' in /node_modules/@contentlayer/core/dist as file
 at resolve esm file path
 at file dependencies /node_modules/@contentlayer/core/dist/mdx.js
 at file /node_modules/@contentlayer/core/dist/mdx.js
 at resolve esm file ./mdx.js
 at file dependencies /node_modules/@contentlayer/core/dist/index.js
 at file /node_modules/@contentlayer/core/dist/index.js
 at resolve esm file @contentlayer/core
 at file dependencies /node_modules/next-contentlayer/dist/plugin.js
 at file /node_modules/next-contentlayer/dist/plugin.js
 at resolve esm file ./plugin.js
 at file dependencies /node_modules/next-contentlayer/dist/index.js
 at file /node_modules/next-contentlayer/dist/index.js
 at resolve esm file next-contentlayer
 at file dependencies /apps/web/next.config.mjs
 at file /apps/web/next.config.mjs
 at resolve commonjs /apps/web/next.config.mjs

Package versions:

  • contentlayer: v0.1.1
  • next: v12.1.0
  • next-contentlayer: v0.1.1
  • react: v17.0.2
  • typescript: v4.5.5

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Example next.config.js, for the next person who comes along and sees this:

const { withContentlayer } = require("next-contentlayer");

module.exports = withContentlayer();

So since ContentLayer doesn’t work correctly with ESM/next.config.mjs, that means it’s incompatible with the plaiceholder package, which requires ESM/next.config.mjs.

Shame. I hope this is resolved at some point.

File updated: 2023-07-02-playwright-logo.mdx
✘ [ERROR] Could not resolve "path"

    ../../node_modules/.pnpm/next@13.4.7_react-dom@18.2.0_react@18.2.0_sass@1.63.6/node_modules/next/dist/compiled/micromatch/index.js:22:3444:
      22 │ ...ge},17:e=>{e.exports=require("path")},837:e=>{e.exports=require...
         ╵                                 ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "util"

    ../../node_modules/.pnpm/next@13.4.7_react-dom@18.2.0_react@18.2.0_sass@1.63.6/node_modules/next/dist/compiled/micromatch/index.js:22:3479:
      22 │ ...)},837:e=>{e.exports=require("util")}};var t={};function __nccw...
         ╵                                 ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

Error: Found 1 problems in 1 documents.

 └── Encountered unexpected errors while processing of 1 documents. This is possibly a bug in Contentlayer. Please open an issue.

     • "2023-07-02-playwright-logo.mdx": UnexpectedMDXError: Error: Build failed with 2 errors:
     ../../node_modules/.pnpm/next@13.4.7_react-dom@18.2.0_react@18.2.0_sass@1.63.6/node_modules/next/dist/compiled/micromatch/index.js:22:3444: ERROR: Could not resolve "path"
     ../../node_modules/.pnpm/next@13.4.7_react-dom@18.2.0_react@18.2.0_sass@1.63.6/node_modules/next/dist/compiled/micromatch/index.js:22:3479: ERROR: Could not resolve "util"

Still getting this error.

This is my MDX:

---
author: luc-gagan
description: "Playwright logos and brand assets for press and media use."
guid: 0189174d-eed1-78d6-ae06-1a117062f130
publishedAt: 2023-07-02T15:52:03.773Z
slug: playwright-logo
tags: [playwright, playwright-test]
title: "Playwright logos and brand assets"
altTitle: "Press & media resources"
---

import { BrandAssets } from '../components/BrandAssets.tsx';

My next config is already wrapped with withContentlayer.

What could be off?

Same issue here. The problem occurs when using a next.config.mjs file. When I switch to an identical next.config.js there is no problem.

Reproduction here: https://github.com/mshick/newnewsite

I’ve taken a deeper look into this issue and the provided repro (thanks @mshick). Here is what I’ve found:

  1. Incorrect Next.js plugin usage

It seems like the original issue was related to the usage of the Next.js plugin and was by @brunowego by using export default withContentlayer()(nextConfig) in the Next config.

To avoid other users potentially running into this problem I’m planning to introduce this API change for the Next.js plugin: https://github.com/contentlayerdev/contentlayer/issues/140 (Feedback wanted!)

  1. Next.js warning

Additionally to the original problem (see (1)) there seems to be another problem (which technically is only a “warning”) which isn’t a problem in Contentlayer itself but rather a bug in Next.js / Webpack (see https://github.com/vercel/next.js/issues/33693) when using next.config.mjs instead of next.config.js. My understanding is that this issue will be addressed by the Next.js team very soon.

If you don’t want to wait until this problem is addressed in Next.js, please use next.config.js (via CommonJS) for now.


Given (1) was successfully solved and (2) is an external problem with known workaround, I’ll close this issue. If there are any further problems related to this, please provide further context in the comments or on Discord.

@schickling appears this was closed without resolution

I’ll take a more in-depth look next week when I’m working on Contentlayer again but in the meanwhile you can try to change this line https://github.com/brunowego/issue-reproduction/blob/next-contentlayer-129/next.config.mjs#L10 to

export default withContentlayer()(nextConfig)

I also having the same issue here like saying -webpack.cache.PackFileCacheStrategy/webpack.FileSystemInfo] Parsing of /home/kinfish/loglib/node_modules/.pnpm/@contentlayer+core@0.3.1_esbuild@0.17.0/node_modules/@contentlayer/core/dist/dynamic-build.js for build dependencies failed at 'import(file://${compiledConfigPath}`

same error,how to fix it?