next.js: libraries using `__dirname` don't resolve correctly in server components

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.17.1
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.3-canary.0
      eslint-config-next: 13.3.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.2

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/gifted-einstein-mo56ss?file=%2Fnext.config.js%3A19%2C7

To Reproduce

  • View the code sandbox
  • See that both the app dir and pages dir are being used, and are calling the same function
  • Browse to /. See that the page loads
  • Browse to /foo. See that the page errors, due to incorrectly deriving the path to the WASM file

Describe the Bug

The reproduction uses a library, @emurgo/cardano-sererialization-lib that’s compiled with wasm-pack. Wasm-pack saves the .WASM file, and loads it by deriving the path using this compiled code:

const path = require('path').join(__dirname, 'cardano_serialization_lib_bg.wasm');

As a hack to see if i could fix it, I edited the compiled lib to use this, which works correctly:

const path = require('path').join(process.cwd(), "node_modules", "@emurgo", "cardano-serialization-lib-nodejs", 'cardano_serialization_lib_bg.wasm');

Expected Behavior

I expect it to “just work” in both the app and pages directory

This issue could be fixed by the underlying libraries like wasm-pack, and I originally opened an issue there, but it seems like this may be a larger problem with nextjs itself

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

@mPaella the fix for your issue just got merged. So make sure to bump your project to the next canary release (as in, the next release from this moment) to be able to run your project without the before mentioned manual experimental config👌

Edit: just checked real quick and it’s already pushed into the latest canary release🤙

@mPaella btw, can you keep this issue open by chance till its fixed upstream without the experimental manual config?

@mPaella just submitted the PR 😉 So if everything goes smoothly you’ll be able to run your project on the canary release after this PR gets merged without the before mentioned config entry 👍

I went with this

serverComponentsExternalPackages: ["@blockfrost/blockfrost-js", "@jpg-store/lucid-cardano", "mongoose"],

I’ll open a PR tomorrow that fixes this issue in the future👌

In your Next.js config, add the “@emurgo/cardano-serialization-lib-nodejs” to the “experimantal” config entry “serverComponentsExternalPackages” to check if it still throws this same error when added to this config option.

Ah i see, didn’t know that was an option. That does appear to be working after altering the reproduction.

I will try in our production app and see if its working there as well.