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

libraries using __dirname don't resolve correctly in server components #49783

Closed
1 task done
mPaella opened this issue May 14, 2023 · 17 comments · Fixed by #49938
Closed
1 task done

libraries using __dirname don't resolve correctly in server components #49783

mPaella opened this issue May 14, 2023 · 17 comments · Fixed by #49938
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.

Comments

@mPaella
Copy link
Contributor

mPaella commented May 14, 2023

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

@mPaella mPaella added the bug Issue was opened via the bug report template. label May 14, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label May 14, 2023
@JesseKoldewijn
Copy link
Contributor

In the case you're using typescript you probably don't have the esModuleInterop enabled. Could you check if this fixes your issue by chance? https://www.typescriptlang.org/tsconfig#esModuleInterop

@mPaella
Copy link
Contributor Author

mPaella commented May 15, 2023

@JesseKoldewijn we do already have that in our tsconfig

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "target": "es5",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "baseUrl": ".",
        "paths": {
            "@/components/*": ["components/*"],
            "@/config/*": ["config/*"],
            "@/utils/*": ["utils/*"],
            "@/layouts/*": ["layouts/*"],
            "@/storage/*": ["storage/*"],
            "@/models/*": ["models/*"],
            "@/services/*": ["services/*"],
            "@/types/*": ["types/*"],
            "@/modules/*": ["modules/*"],
            "@/consts/*": ["consts/*"],
            "@/hooks/*": ["hooks/*"],
            "@/styles/*": ["styles/*"],
            "@/classes/*": ["classes/*"],
            "@/pages/*": ["pages/*"],
            "@/emailTemplates/*": ["emailTemplates/*"],
            "@/cypress/*": ["cypress/*"],
            "@/blockchain/*": ["blockchain/*"],
            "@/middleware/*": ["middleware/*"],
            "@/tests/*": ["tests/*"]
        },
        "plugins": [
            {
                "name": "next"
            }
        ]
    },
    "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "types/.d.ts", "migrations/*.ts", ".next/types/**/*.ts"],
    "exclude": ["node_modules"],
    "ts-node": {
        "require": ["tsconfig-paths/register"],
        "compilerOptions": {
            "module": "CommonJS"
        }
    }
}

@JesseKoldewijn
Copy link
Contributor

Does do you yourself give it a path or does it do this internally in the package itself?

@mPaella
Copy link
Contributor Author

mPaella commented May 15, 2023

The path is generated by wasm-pack, so inside the library when it's built/released

You can view it in the compiled bundle (go here => click "code" => open cardano_serialization_lib.js => go to line 27629)

image

@JesseKoldewijn
Copy link
Contributor

Alright check! Could you maybe try the following.

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.

@mPaella
Copy link
Contributor Author

mPaella commented May 15, 2023

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.

@mPaella mPaella closed this as completed May 17, 2023
@mPaella
Copy link
Contributor Author

mPaella commented May 17, 2023

@JesseKoldewijn I can confirm adding the libraries to experimental.serverComponentsExternalPackages has solved the problem for me

@JesseKoldewijn
Copy link
Contributor

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

@JesseKoldewijn
Copy link
Contributor

@mPaella did you only add the package in question to that config entry or also wasm-pack?

@mPaella
Copy link
Contributor Author

mPaella commented May 17, 2023

I went with this

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

@JesseKoldewijn
Copy link
Contributor

@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 👍

@mPaella
Copy link
Contributor Author

mPaella commented May 17, 2023

Awesome!

Would love to know what the longer term solution here is as well. What's the difference in compilation that's causing these packages to be added to the exclusions list?

What can libraries do to avoid this?

@JesseKoldewijn
Copy link
Contributor

Awesome!

Would love to know what the longer term solution here is as well. What's the difference in compilation that's causing these packages to be added to the exclusions list?

What can libraries due to avoid this?

As far as I know it has to do with how the App Router is compiled. As for how to prevent this, not sure. Haven't dug that deep into it to be honest. Plus I personally haven't ran into a package in the projects that I work on where they run into these compile issue. I mainly recognized this behavior because bcrypt also used to have issues in the App Router before I PR'ed it into this external package list. ;)

But, I'm going to move to a Next.js focused job soon so I'm sure I'll be of more used by that time with regards to more deep stuff than just these small fixes which are just pattern recognition in my mind right now.

@JesseKoldewijn
Copy link
Contributor

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

@mPaella mPaella reopened this May 23, 2023
@JesseKoldewijn
Copy link
Contributor

The PR unfortunately is still open in review (just synced it with upstream to make merging for the core team easier). So keep an eye on PR #49938 I would expect it can be merged soon since its just a two-line addition.

timneutkens pushed a commit that referenced this issue May 23, 2023
…ackages to prevent issue #49783 from happening. (#49938)

fixes #49783

### What?
Added the following packages to the server component exclusion list
which prevents these packages from going through the currently default
compile pipeline for apps that use the App Router. Packages added are:
"@blockfrost/blockfrost-js", "@jpg-store/lucid-cardano" and "mongoose".
(For instance, mongo was already in this exclusion list)

### Why?
These packages are required by the
"@emurgo/cardano-serialization-lib-nodejs" packages and break when not
added to this list while using the app router.

### How?
I've added these packages to the server-external-packages json file in
the Next.js package's lib directory.
@JesseKoldewijn
Copy link
Contributor

JesseKoldewijn commented May 23, 2023

@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🤙

hydRAnger pushed a commit to hydRAnger/next.js that referenced this issue Jun 12, 2023
…ackages to prevent issue vercel#49783 from happening. (vercel#49938)

fixes vercel#49783

### What?
Added the following packages to the server component exclusion list
which prevents these packages from going through the currently default
compile pipeline for apps that use the App Router. Packages added are:
"@blockfrost/blockfrost-js", "@jpg-store/lucid-cardano" and "mongoose".
(For instance, mongo was already in this exclusion list)

### Why?
These packages are required by the
"@emurgo/cardano-serialization-lib-nodejs" packages and break when not
added to this list while using the app router.

### How?
I've added these packages to the server-external-packages json file in
the Next.js package's lib directory.
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.
Projects
None yet
2 participants