Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build dependencies failed at import and can't resolve 'path' #129

Closed
brunowego opened this issue Feb 21, 2022 · 22 comments
Closed

Build dependencies failed at import and can't resolve 'path' #129

brunowego opened this issue Feb 21, 2022 · 22 comments

Comments

@brunowego
Copy link

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
@schickling
Copy link
Collaborator

Hi @brunowego! Thanks for opening this issue. Please provide a link to a reproduction repository. 🙏

@brunowego
Copy link
Author

Hi @schickling, thanks for you quick reply. Sure, take a look here https://github.com/brunowego/issue-reproduction/tree/next-contentlayer-129

@schickling
Copy link
Collaborator

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)

@schickling
Copy link
Collaborator

@brunowego I've just given the repo you posted a try and it seems to work for me. Is there still a problem?

@brunowego
Copy link
Author

brunowego commented Feb 28, 2022

Hi @schickling, yes, nothing changed. Using NPM or Yarn commands got the same issue.


Versions:

  • yarn v1.22.17
  • npm v8.5.1

@stale stale bot removed the meta: stale label Feb 28, 2022
@schickling
Copy link
Collaborator

I unfortunately can't reproduce the problem with the repo you shared. Can you please try to delete .next and node_modules and try again? (Ideally with a little video demo.)

@brunowego
Copy link
Author

Thanks a lot for your time! I really understand that is hard to reproduce this error. Yes, I removed the both folders and the warning continues.

@auser
Copy link

auser commented Mar 10, 2022

I'm getting this error as well on the latest "contentlayer": "^0.0.35-dev.5", branch.

It also occurs on: "^0.1.2"

Relevant versions:

"contentlayer": "^0.1.2",
"convertkit-react": "^1.5.5",
❯ node --version
v16.13.0

Did you find/fix this error, by chance?

@schickling
Copy link
Collaborator

@auser thanks for your comment. Would you please mind posting a link to your repo with instructions how to reproduce this problem?

@mshick
Copy link
Contributor

mshick commented Mar 11, 2022

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

@mshick
Copy link
Contributor

mshick commented Mar 11, 2022

I'll add, I get an additional error on startup which I've pasted below.

I've removed .next, node_modules, package-lock.json and .contentlayer per your instructions, and fully reinstalled — the issue persists. I'm on node v14.19.0, npm 6.14.16.

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

@frencojobs
Copy link

I'm also getting pretty much the same warnings in logs when using with next.config.mjs. I'm on node v16.14.0 with yarn v1.22.17.

See logs
event - compiled client and server successfully in 2.9s (146 modules)
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'fs' in '/oss/frenco.dev/node_modules/@contentlayer/utils/dist/node'
<w> while resolving 'fs' in /oss/frenco.dev/node_modules/@contentlayer/utils/dist/node as file
<w>  at resolve esm file fs
<w>  at file dependencies /oss/frenco.dev/node_modules/@contentlayer/utils/dist/node/fs.js
<w>  at file /oss/frenco.dev/node_modules/@contentlayer/utils/dist/node/fs.js
<w>  at resolve esm file ./fs.js
<w>  at file dependencies /oss/frenco.dev/node_modules/@contentlayer/utils/dist/node/index.js
<w>  at file /oss/frenco.dev/node_modules/@contentlayer/utils/dist/node/index.js
<w>  at resolve esm file @contentlayer/utils/node
<w>  at file dependencies /oss/frenco.dev/node_modules/@contentlayer/core/dist/ArtifactsDir.js
<w>  at file /oss/frenco.dev/node_modules/@contentlayer/core/dist/ArtifactsDir.js
<w>  at resolve esm file ./ArtifactsDir.js
<w>  at file dependencies /oss/frenco.dev/node_modules/@contentlayer/core/dist/index.js
<w>  at file /oss/frenco.dev/node_modules/@contentlayer/core/dist/index.js
<w>  at resolve esm file @contentlayer/core
<w>  at file dependencies /oss/frenco.dev/node_modules/next-contentlayer/dist/plugin.js
<w>  at file /oss/frenco.dev/node_modules/next-contentlayer/dist/plugin.js
<w>  at resolve esm file ./plugin.js
<w>  at file dependencies /oss/frenco.dev/node_modules/next-contentlayer/dist/index.js
<w>  at file /oss/frenco.dev/node_modules/next-contentlayer/dist/index.js
<w>  at resolve esm file next-contentlayer
<w>  at file dependencies /oss/frenco.dev/next.config.mjs
<w>  at file /oss/frenco.dev/next.config.mjs
<w>  at resolve commonjs /oss/frenco.dev/next.config.mjs
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'path' in '/oss/frenco.dev/node_modules/@contentlayer/utils/dist'
<w> while resolving 'path' in /oss/frenco.dev/node_modules/@contentlayer/utils/dist as file
<w>  at resolve esm file path
<w>  at file dependencies /oss/frenco.dev/node_modules/@contentlayer/utils/dist/file-paths.js
<w>  at file /oss/frenco.dev/node_modules/@contentlayer/utils/dist/file-paths.js
<w>  at resolve esm file ./file-paths.js
<w>  at file dependencies /oss/frenco.dev/node_modules/@contentlayer/utils/dist/index.js
<w>  at file /oss/frenco.dev/node_modules/@contentlayer/utils/dist/index.js
<w>  at resolve esm file @contentlayer/utils
<w>  at file dependencies /oss/frenco.dev/node_modules/next-contentlayer/dist/plugin.js
<w>  at file /oss/frenco.dev/node_modules/next-contentlayer/dist/plugin.js
<w>  at resolve esm file ./plugin.js
<w>  at file dependencies /oss/frenco.dev/node_modules/next-contentlayer/dist/index.js
<w>  at file /oss/frenco.dev/node_modules/next-contentlayer/dist/index.js
<w>  at resolve esm file next-contentlayer
<w>  at file dependencies /oss/frenco.dev/next.config.mjs
<w>  at file /oss/frenco.dev/next.config.mjs
<w>  at resolve commonjs /oss/frenco.dev/next.config.mjs

@schickling
Copy link
Collaborator

schickling commented Mar 28, 2022

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: #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 vercel/next.js#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.

@JoeDuncko
Copy link

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

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

module.exports = withContentlayer();

@lucgagan
Copy link

lucgagan commented Jul 2, 2023

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?

@aafnnp
Copy link

aafnnp commented Aug 2, 2023

same error,how to fix it?

@itsfaraaz
Copy link

Also receiving the same error as @lucgagan

@i-bsd
Copy link

i-bsd commented Aug 16, 2023

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.

@Kinfe123
Copy link

Kinfe123 commented Aug 23, 2023

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}`

@lucgagan
Copy link

@schickling appears this was closed without resolution

@lucgagan
Copy link

For what it is worth, migrating the entire project back to commonjs makes the issue go away.

Seo0H added a commit to Seo0H/seo0h.github.io that referenced this issue Oct 5, 2023
@contawo
Copy link

contawo commented Feb 16, 2024

I have been tryna solve the problem from my side also. But first I wanted to understand what could be the issue.

Client side and server side

Since contentlayer compiles or renders or whatever it does, it does it on the server side, not the client side. I am using NextJS version 14 with TypeScript. When I use "use client", I get this error. But when it is a server component, I do not get the error.

A way to solve the problem

If you are using the useMDXComponent and you are overriding your components, there you can customize the elements with client side components.
So what I did is create a new element on the mdx file:

some content here 
<comp prop={any} />

Then I added the new "element" to my customized components, like this:

...
comp: ({...props }) => (
    <ClientSideComponent
      props={props} 
    />
 )

Here is how your client side will look like:

"use client";

import React from "react";

export default function ClientSideComponent({ props }: {props: {[x: string]: any;}}) {
    return (
        ...jsx
    )
}

So instead of using your client side component directly on the mdx file, like this:

import ClientSideComponent from "../path/ClientSideComponent";

Some conent

<ClientSideComponent  />

You just use the way I showed. The code is type safe if you are using TypeScript. Hope this helps, if it does not, no worries your a re programmer, you will figure it out 🥴.

lee-sihun added a commit to lee-sihun/sihuns-blog that referenced this issue May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests