Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Importing zx library in RSC causes bundler error #76637

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

Open
jonathanhefner opened this issue Feb 27, 2025 · 3 comments
Open

Importing zx library in RSC causes bundler error #76637

jonathanhefner opened this issue Feb 27, 2025 · 3 comments
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. Turbopack Related to Turbopack with Next.js. Webpack Related to Webpack with Next.js.

Comments

@jonathanhefner
Copy link
Contributor

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/priceless-forest-xjdq86

To Reproduce

  1. Run pnpm install zx.

  2. Reference any zx function in a server component (or in library code called by a server component). It is only necessary to reference the function, not call it. For example:

    import { glob } from "zx";
    
    export default async function Page() {
      return typeof glob;
    }
  3. Visit the page, and observe the "ENOENT: no such file or directory, open '/project/workspace/.next/server/package.json'" error.

    Full stack trace
    __webpack_require__
    file:/project/workspace/.next/server/webpack-runtime.js (33:42)
    
    __webpack_require__
    file:/project/workspace/.next/server/webpack-runtime.js (33:42)
    
    eval
    ./app/page.tsx
    
    <unknown>
    rsc)/./app/page.tsx (/project/workspace/.next/server/app/page.js (33:1)
    
    Function.__webpack_require__
    file:/project/workspace/.next/server/webpack-runtime.js (33:42)
    
    Object.open
    node:internal/fs/sync (78:18)
    
    Object.openSync
    node:fs (565:17)
    
    Object.readFileSync
    node:fs (445:35)
    
    Object.readFileSync
    node_modules/.pnpm/[email protected]/node_modules/zx/build/vendor-extra.cjs (7756:1)
    
    eval
    node_modules/.pnpm/[email protected]/node_modules/zx/build/index.cjs (45:1)
    
    <unknown>
    rsc)/./node_modules/.pnpm/[email protected]/node_modules/zx/build/index.cjs (/project/workspace/.next/server/vendor-chunks/[email protected] (63:1)
    
    eval
    ./node_modules/.pnpm/[email protected]/node_modules/zx/build/index.js?6b75
    
    <unknown>
    rsc)/./node_modules/.pnpm/[email protected]/node_modules/zx/build/index.js?6b75 (/project/workspace/.next/server/vendor-chunks/[email protected] (74:1)
    
    async getLayoutOrPageModule
    next/dist/src/server/lib/app-dir-module.ts (29:11)
    
    async createComponentTreeInternal
    next/dist/src/server/app-render/create-component-tree.tsx (181:45)
    
    async
    next/dist/src/server/app-render/create-component-tree.tsx (511:28)
    
    async createComponentTreeInternal
    next/dist/src/server/app-render/create-component-tree.tsx (437:28)
    
    async getRSCPayload
    next/dist/src/server/app-render/app-render.tsx (843:20)
    
    async renderToStream
    next/dist/src/server/app-render/app-render.tsx (1857:26)
    
    async renderToHTMLOrFlightImpl
    next/dist/src/server/app-render/app-render.tsx (1556:20)
    
    async doRender
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (2779:22)
    
    async responseGenerator
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (3139:22)
    
    async DevServer.renderToResponseWithComponentsImpl
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (3152:24)
    
    async DevServer.renderPageComponent
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (3733:16)
    
    async DevServer.renderToResponseImpl
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (3795:24)
    
    async DevServer.pipeImpl
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (1779:21)
    
    async NextNodeServer.handleCatchallRenderRequest
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/next-server.ts (1087:7)
    
    async DevServer.handleRequestImpl
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/base-server.ts (1531:9)
    
    async
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/dev/next-dev-server.ts (512:14)
    
    async Span.traceAsyncFn
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/trace/trace.ts (143:14)
    
    async DevServer.handleRequest
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/dev/next-dev-server.ts (510:20)
    
    async invokeRender
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/lib/router-server.ts (291:11)
    
    async handleRequest
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/lib/router-server.ts (537:16)
    
    async requestHandlerImpl
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/lib/router-server.ts (583:7)
    
    async Server.requestListener
    node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/src/server/lib/start-server.ts (154:7)
    

Current vs. Expected behavior

I expect to be able to use zx in server-side code.

The current behavior causes an ENOENT error when any zx export is referenced.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4242
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 15.2.1-canary.1 // Latest available version is detected (15.2.1-canary.1).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack, Webpack

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local)

Additional context

(This is a retry of #76636, which the bot erroneously closed.)

zx is a library by Google that provides various utility functions for interacting with the file system and OS. For example, a glob function and functions for colorizing log messages.

@github-actions github-actions bot added Turbopack Related to Turbopack with Next.js. Webpack Related to Webpack with Next.js. labels Feb 27, 2025
Copy link
Contributor

We could not detect a valid reproduction link. Make sure to follow the bug report template carefully.

Why was this issue closed?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We need a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

The bug template that you filled out has a section called "Link to the code that reproduces this issue", which is where you should provide the link to the reproduction.

  • If you did not provide a link or the link you provided is not valid, we will close the issue.
  • If you provide a link to a private repository, we will close the issue.
  • If you provide a link to a repository but not in the correct section, we will close the issue.

What should I do?

Depending on the reason the issue was closed, you can do the following:

  • If you did not provide a link, please open a new issue with a link to a reproduction.
  • If you provided a link to a private repository, please open a new issue with a link to a public repository.
  • If you provided a link to a repository but not in the correct section, please open a new issue with a link to a reproduction in the correct section.

In general, assume that we should not go through a lengthy onboarding process at your company code only to be able to verify an issue.

My repository is private and cannot make it public

In most cases, a private repo will not be a sufficient minimal reproduction, as this codebase might contain a lot of unrelated parts that would make our investigation take longer. Please do not make it public. Instead, create a new repository using the templates above, adding the relevant code to reproduce the issue. Common things to look out for:

  • Remove any code that is not related to the issue. (pages, API routes, components, etc.)
  • Remove any dependencies that are not related to the issue.
  • Remove any third-party service that would require us to sign up for an account to reproduce the issue.
  • Remove any environment variables that are not related to the issue.
  • Remove private packages that we do not have access to.
  • If the issue is not related to a monorepo specifically, try to reproduce the issue without a complex monorepo setup

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps by opening a new issue.

I think my reproduction is good enough, why aren't you looking into it quickly?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@github-actions github-actions bot added the invalid link The issue was auto-closed due to a missing/invalid reproduction link. A new issue should be opened. label Feb 27, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Feb 27, 2025
@samcx samcx removed the invalid link The issue was auto-closed due to a missing/invalid reproduction link. A new issue should be opened. label Mar 1, 2025
@vercel vercel unlocked this conversation Mar 1, 2025
@samcx samcx reopened this Mar 1, 2025
@icyJoseph
Copy link
Contributor

icyJoseph commented Mar 1, 2025

It's cuz that package does this:

export const VERSION: string = fs.readJsonSync(
  new URL('../package.json', import.meta.url)
).version

Because the page file is compiled, and run from within the .next folder... I guess that inlines zx code into the .next/server/page whatever destination, and then it tries to find its own version, but the package.json is long gone... see: #74614

There's a solution though!

/** @type {import('next').NextConfig} */
const nextConfig = {
  serverExternalPackages: ['zx'],
};

Relevant docs: https://nextjs.org/docs/app/api-reference/config/next-config-js/serverExternalPackages

@jonathanhefner
Copy link
Contributor Author

Copying my reply from #76639 (reply in thread):

Interesting! And unfortunate. I can appreciate the optimization aspect, but it seems like Next.js (or webpack?) is making unsafe assumptions about the code it's touching. I was lucky to run into this particular error during dev, but there could be similar errors that go undiscovered until they are hit in production.


@icyJoseph

Or the other way around really. There's a section about this in the docs: https://nextjs.org/docs/app/api-reference/config/next-config-js/serverExternalPackages

serverExternalPackages does not negate the risk that this behavior can unexpectedly bite you. Yes, if you know that a package relies on specific Node.js features, you can use serverExternalPackages. The problem is when you don't know, and you don't realize it until you hit the error in production.

I think unsafe behavior should be opt-in instead of opt-out. At the very least, Next.js / webpack should use more heuristics, such as checking for usage of import.meta.url, to automatically exclude packages from this behavior.

@timneutkens timneutkens added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. Turbopack Related to Turbopack with Next.js. Webpack Related to Webpack with Next.js.
Projects
None yet
Development

No branches or pull requests

4 participants