Next.js API routes do not work with amplify.

0

I followed the procedure "Deploy and host a hybrid app (SSG and SSR)" on the official website below.
https://docs.amplify.aws/guides/hosting/nextjs/q/platform/js#api-routes
※I am using the ap-northeast-1 region.

Deployment was successful and the contents of index.js are displayed correctly.

However, when I run "api/hello", it returns "503 Error ,The request could not be satisfied.

I checked my CloudWatch Logs and got the following error message.

2021-06-11T07:42:57.388Z	a9fd33a2-9ba1-4085-bcb4-fde78f4cafd8	ERROR	Invoke Error 	{  
    "errorType": "Error",  
    "errorMessage": "Cannot find module '../../chunks/400.js'\nRequire stack:\n- /var/task/pages/api/hello.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",  
    "code": "MODULE_NOT_FOUND",  
    "requireStack": \[  
        "/var/task/pages/api/hello.js",  
        "/var/task/index.js",  
        "/var/runtime/UserFunction.js",  
        "/var/runtime/index.js"  
    ],  
    "stack": \[  
        "Error: Cannot find module '../../chunks/400.js'",  
        "Require stack:",  
        "- /var/task/pages/api/hello.js",  
        "- /var/task/index.js",  
        "- /var/runtime/UserFunction.js",  
        "- /var/runtime/index.js",  
        "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)",  
        "    at Function.Module._load (internal/modules/cjs/loader.js:667:27)",  
        "    at Module.require (internal/modules/cjs/loader.js:887:19)",  
        "    at require (internal/modules/cjs/helpers.js:74:18)",  
        "    at Object.__webpack_require__.f.require (/var/task/pages/api/hello.js:355:28)",  
        "    at /var/task/pages/api/hello.js:289:40",  
        "    at Array.reduce (<anonymous>)",  
        "    at Function.__webpack_require__.e (/var/task/pages/api/hello.js:288:67)",  
        "    at Function.__webpack_require__.x (/var/task/pages/api/hello.js:371:33)",  
        "    at /var/task/pages/api/hello.js:381:57"  
    ]  
}  

Error (Error: Cannot find module '. /.. I know what the error (Error: Cannot find module '.../.../chunks/400.js') means, but I don't know how to solve it.

Do I need to configure anything in the build or deployment?

Edited by: wiselucky on Jun 11, 2021 12:50 AM

Edited by: wiselucky on Jun 11, 2021 1:07 AM

asked 3 years ago1840 views
2 Answers
0

It was a difference in the nextjs version.
I solved the problem by downgrading v10 to v9 and deploying it.

answered 3 years ago
0

No need to downgrade, just use webpack 4

answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions