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

Serverless API routes not working #7888

Closed
ecwyne opened this issue Jul 10, 2019 · 15 comments · Fixed by #7898
Closed

Serverless API routes not working #7888

ecwyne opened this issue Jul 10, 2019 · 15 comments · Fixed by #7898
Labels
Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).
Milestone

Comments

@ecwyne
Copy link

ecwyne commented Jul 10, 2019

Working in development, but when deploying to now every api route has the following error in the logs.

TypeError: require is not a function
    at Object.+nUH (/var/task/page.js:12162:14)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.ubcj (/var/task/page.js:432129:20)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.ug46 (/var/task/page.js:432406:20)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.1XFb (/var/task/page.js:27759:15)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.qEY2 (/var/task/page.js:410688:11)
    at __webpack_require__ (/var/task/page.js:23:31)
@Timer Timer transferred this issue from another repository Jul 10, 2019
@Timer Timer changed the title [now-next] api routes not working in production Serverless API routes not working Jul 10, 2019
@Timer Timer added this to the 9.0.1 milestone Jul 10, 2019
@Timer
Copy link
Member

Timer commented Jul 10, 2019

Hi @ecwyne, can you please try next@canary?

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

I am using "next": "^9.0.1-canary.1"

next dev 👍
next build && next start 👎
@now/next (running in zeit/now) 👎

@cj
Copy link

cj commented Jul 11, 2019

I've run into an issue with the API routes too (let me know if you want me to open a new ticket if it's not related). I have a file pages/api/pages/index, using next or now dev locally the URL /api/pages/index works just fine, but deployed to now /api/pages/index gives a 404 and this error in the console:

START RequestId: a21f4827-7f66-4b71-8b18-56d03d03a73a Version: $LATEST
2019-07-11T01:05:47.957Z	a21f4827-7f66-4b71-8b18-56d03d03a73a	ERROR	{ FetchError: invalid json response body at https://hulsa-cj.hulsa.now.sh/api/pages/index reason: Unexpected token < in JSON at position 0
    at /var/task/page.js:21277:32
    at process._tickCallback (internal/process/next_tick.js:68:7)
  message:
   'invalid json response body at https://hulsa-cj.hulsa.now.sh/api/pages/index reason: Unexpected token < in JSON at position 0',
  type: 'invalid-json' }
2019-07-11T01:05:47.958Z	a21f4827-7f66-4b71-8b18-56d03d03a73a	ERROR	TypeError: Cannot read property 'experimentalPrerender' of undefined
    at renderToHTML (/var/task/page.js:10728:43)
    at process._tickCallback (internal/process/next_tick.js:68:7)
END RequestId: a21f4827-7f66-4b71-8b18-56d03d03a73a
REPORT RequestId: a21f4827-7f66-4b71-8b18-56d03d03a73a	

/api/pages however, works just fine

@huv1k
Copy link
Contributor

huv1k commented Jul 11, 2019

Hey @ecwyne, could you provide better reproduction? So we can test it and fix it? Thx in advance

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

@huv1k creating a minimum reproduction this morning I discovered that the issue first arises when I add https://github.com/fauna/faunadb-js and target: 'serverless'. Works perfectly when target: 'server'.

I'm happy to open an issue with them if it's an issue on their end. It appears it's an issue because they're using require (which seems totally normal to me). They have a browserify step, but I don't think it's used when importing from npm

@Timer
Copy link
Member

Timer commented Jul 11, 2019

Having the reproduction would still be immensely helpful @ecwyne!

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

@Timer @huv1k error logs point to https://github.com/ecwyne/nextjs-issue-7888/blob/master/www/.next/serverless/pages/api/hello.js#L101 (Line 101)

TypeError: require is not a function
    at Object.+nUH (/var/task/page.js:101:14)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.ubcj (/var/task/page.js:16498:20)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.ug46 (/var/task/page.js:16516:20)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.1XFb (/var/task/page.js:1477:15)
    at __webpack_require__ (/var/task/page.js:23:31)
    at Object.qEY2 (/var/task/page.js:15833:11)
    at __webpack_require__ (/var/task/page.js:23:31)

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

Found this fauna/faunadb-js#133

Which led me to node-formidable/formidable#337

And the suggested solution of node-formidable/formidable#337 (comment)

plugins: [
     new webpack.DefinePlugin({ "global.GENTLY": false })
],

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

Confirmed, updating my next.config.js to the following worked (for me).

const webpack = require('webpack');

module.exports = {
    target: 'serverless',
    webpack: config => {
        config.plugins.push(
            new webpack.DefinePlugin({ 'global.GENTLY': false }),
        );
        return config;
    },
};

@Timer
Copy link
Member

Timer commented Jul 11, 2019

I guess we can close this then -- it's very unfortunate that formidable is unmaintained and incompatible with the Babel/Webpack ecosystem. 😞

Can you open an issue with faundadb to move to something that's maintained?

@Timer Timer closed this as completed Jul 11, 2019
@Timer
Copy link
Member

Timer commented Jul 11, 2019

Actually, it probably doesn't hurt for us to define this. PR incoming.

@ecwyne
Copy link
Author

ecwyne commented Jul 11, 2019

Thanks so much @Timer! Everything in Next v9 is INCREDIBLE!

@Timer Timer added Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.). and removed Type: Needs Investigation labels Jul 11, 2019
@Timer
Copy link
Member

Timer commented Jul 11, 2019

Thank you for the awesome detective work @ecwyne!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants