martpie / next-transpile-modules

Next.js plugin to transpile code from node_modules. Please see: https://github.com/martpie/next-transpile-modules/issues/291
MIT License
1.13k stars 85 forks source link

Path resolution errors on Preview-Mode API-Route, on Remote only #234

Closed lunelson closed 2 years ago

lunelson commented 2 years ago

Are you trying to transpile a local package or an npm package?

I'm trying to transpile my own npm package @rasahq/react-tabula

Describe the bug

I'm working on a site that is configured with a basePath. All works correctly for both dev and build, locally and remotely, but fails when I trigger an API path on the remote, resulting in a server error 500.

I can't reproduce this error in any other environment, the /api/preview path works correctly when built and served on my machine. The following is the log from Vercel on my own production site (but I've made a minimal reproduction deployment below):

[GET] /blog/api/preview/?path=/blog/intents-entities-understanding-the-rasa-nlu-pipeline/&secret=fUKYxkpFb2JgdxQ4C7Tapw2odM32cXfs
16:01:05:57
next-transpile-modules - DEPRECATED - fallbacking to previous module resolution system for module "@rasahq/react-tabula", you can now just pass the name of the package to transpile and it will detect its real path without you having to pass a sub-module.
2021-09-03T14:01:06.510Z    ff9ab8d4-ae4f-4c4c-bf72-673d3adbbcfc    ERROR   Error: next-transpile-modules - an unexpected error happened when trying to resolve "@rasahq/react-tabula". Are you sure the name module you are trying to transpile is correct, and it has a "main" or an "exports" field?
Error: Can't resolve '@rasahq/react-tabula' in '/var/task'
    at getPackageRootDirectory (/var/task/node_modules/next-transpile-modules/src/next-transpile-modules.js:173:17)
    at Array.map (<anonymous>)
    at withTM (/var/task/node_modules/next-transpile-modules/src/next-transpile-modules.js:185:34)
    at /var/task/.next/server/chunks/64.js:80:57
    at Array.reduce (<anonymous>)
    at composePlugins (/var/task/.next/server/chunks/64.js:78:18)
    at Object.3064 (/var/task/.next/server/chunks/64.js:47:18)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
    at Object.797 (/var/task/.next/server/pages/api/preview.js:25:5)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
RequestId: ff9ab8d4-ae4f-4c4c-bf72-673d3adbbcfc Error: Runtime exited with error: exit status 1
Runtime.ExitError

To Reproduce

I've created a minimal reproduction here:

Expected behavior

The API routes should put the site in to preview mode, and cause the PreviewControls component to show at the bottom of the screen. If you clone and build this site locally you will see this. On the remote it produces a 500 server error.

Try this route for example

Setup

martpie commented 2 years ago

Thank you for the repro! I'll try to have a look asap!

lunelson commented 2 years ago

Looks like I figured it out 🤦🏻

Because I wanted access to the basePath constant, I pulled it in from next.config.js, but this caused that entire module to execute and fail—probably because in the remote serverless function context most of that stuff doesn't apply, though strangely it works on localhost

...so I updated both the API routes and next.config.js to pull the basePath value from somewhere else and it works now—I guess that's the value of doing a minimal reproduction 😅