NextJs Scheduled Functions Error "Cannot find module"

Hello.

Site Url : https://stci-mini-test.netlify.app/

I am building a scheduled function to poll from external service every hour ( even though in this example I am configuring it to run every minute for testing purposes ).
The project is built with no errors, the scheduled function is read correctly by the Functions tab in netlify ( the path to the function is /api/test ).

import { NextApiRequest, NextApiResponse } from 'next'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  console.log(req)

  res.status(200).send({})

  return {
    statusCode: 200,
  }
}

export default handler

export const config = {
  type: 'experimental-scheduled',
  schedule: '* * * * *',
}

At first I had some issures with the middleware auth logic as it was blocking the scheduled requests to the api so I commented out all the logic to test it.

Now the function log is showing the error down below on every call.
As far as I understand netlify is calling a path that does not exist
.netlify/functions-internal/_api_test-handler

Jun 26, 04:19:01 PM: 2023-06-26T15:19:01.030Z undefined ERROR Uncaught Exception {ā€œerrorTypeā€:ā€œRuntime.ImportModuleErrorā€,ā€œerrorMessageā€:ā€œError: Cannot find module ā€˜follow-redirectsā€™\nRequire stack:\n- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.js\n- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.js\n- /var/task/_api_test-handler.js\n- /var/runtime/index.mjsā€,ā€œstackā€:[ā€œRuntime.ImportModuleError: Error: Cannot find module ā€˜follow-redirectsā€™ā€,ā€œRequire stack:ā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.jsā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.jsā€,ā€œ- /var/task/_api_test-handler.jsā€,ā€œ- /var/runtime/index.mjsā€," at _loadUserApp (file:///var/runtime/index.mjs:997:17)ā€œ,ā€ at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)ā€œ,ā€ at async start (file:///var/runtime/index.mjs:1195:23)ā€œ,ā€ at async file:///var/runtime/index.mjs:1201:1"]}Jun 26, 04:19:01 PM: Unknown application error occurred
Runtime.ImportModuleErrorJun 26, 04:19:01 PM: 37d08802 Duration: 202.11 ms Memory Usage: 17 MB Jun 26, 04:19:02 PM: 2023-06-26T15:19:02.270Z undefined ERROR Uncaught Exception {ā€œerrorTypeā€:ā€œRuntime.ImportModuleErrorā€,ā€œerrorMessageā€:ā€œError: Cannot find module ā€˜follow-redirectsā€™\nRequire stack:\n- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.js\n- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.js\n- /var/task/_api_test-handler.js\n- /var/runtime/index.mjsā€,ā€œstackā€:[ā€œRuntime.ImportModuleError: Error: Cannot find module ā€˜follow-redirectsā€™ā€,ā€œRequire stack:ā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.jsā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.jsā€,ā€œ- /var/task/_api_test-handler.jsā€,ā€œ- /var/runtime/index.mjsā€," at _loadUserApp (file:///var/runtime/index.mjs:997:17)ā€œ,ā€ at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)ā€œ,ā€ at async start (file:///var/runtime/index.mjs:1195:23)ā€œ,ā€ at async file:///var/runtime/index.mjs:1201:1"]}Jun 26, 04:19:02 PM: Unknown application error occurred
Runtime.ImportModuleErrorJun 26, 04:19:02 PM: 868de1fc Duration: 200.01 ms Memory Usage: 18 MB Jun 26, 04:19:02 PM: INIT_START Runtime Version: nodejs:18.v7 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:26e9402591d9c467cdcc7355f843d61968f36ecb6e9ef5ab253cc21163343275Jun 26, 04:19:02 PM: 2023-06-26T15:19:02.509Z undefined ERROR Uncaught Exception {ā€œerrorTypeā€:ā€œRuntime.ImportModuleErrorā€,ā€œerrorMessageā€:ā€œError: Cannot find module ā€˜follow-redirectsā€™\nRequire stack:\n- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.js\n- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.js\n- /var/task/_api_test-handler.js\n- /var/runtime/index.mjsā€,ā€œstackā€:[ā€œRuntime.ImportModuleError: Error: Cannot find module ā€˜follow-redirectsā€™ā€,ā€œRequire stack:ā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.jsā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.jsā€,ā€œ- /var/task/_api_test-handler.jsā€,ā€œ- /var/runtime/index.mjsā€," at _loadUserApp (file:///var/runtime/index.mjs:997:17)ā€œ,ā€ at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)ā€œ,ā€ at async start (file:///var/runtime/index.mjs:1195:23)ā€œ,ā€ at async file:///var/runtime/index.mjs:1201:1"]}Jun 26, 04:19:03 PM: 2023-06-26T15:19:03.931Z undefined ERROR Uncaught Exception {ā€œerrorTypeā€:ā€œRuntime.ImportModuleErrorā€,ā€œerrorMessageā€:ā€œError: Cannot find module ā€˜follow-redirectsā€™\nRequire stack:\n- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.js\n- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.js\n- /var/task/_api_test-handler.js\n- /var/runtime/index.mjsā€,ā€œstackā€:[ā€œRuntime.ImportModuleError: Error: Cannot find module ā€˜follow-redirectsā€™ā€,ā€œRequire stack:ā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/handlerUtils.jsā€,ā€œ- /var/task/.netlify/functions-internal/_api_test-handler/_api_test-handler.jsā€,ā€œ- /var/task/_api_test-handler.jsā€,ā€œ- /var/runtime/index.mjsā€," at _loadUserApp (file:///var/runtime/index.mjs:997:17)ā€œ,ā€ at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)ā€œ,ā€ at async start (file:///var/runtime/index.mjs:1195:23)ā€œ,ā€ at async file:///var/runtime/index.mjs:1201:1"]}Jun 26, 04:19:03 PM: Unknown application error occurred
Runtime.ImportModuleErrorJun 26, 04:19:03 PM: 4690a8f5 Duration: 197.70 ms Memory Usage: 18 MB

[ UPDATE ]
I run other tests on that api endpoint ( /api/test )
I added some logic to do side effect ( add a test document in my mongodb database )
The build shows that /api/test is redirected to /404
3:27:26 PM: { from: '/api/test', to: '/404.html', status: 404 }
and the docs says that a scheduled function canā€™t be invoked with the url (Scheduled Functions | Netlify Docs)
But in my case when I enter https://stci-mini-test.netlify.app/api/test I get the 404 as intended however the side-effect runs and I get a new document in my database is that normal ?

Hi, can you attach a link to your function log?

I am not sure how to do that.
I was referring to the netlify function log page. I donā€™t see an option to share the link.

Could you try to deploy again? There have been some changes which could have fixed this.

Well Itā€™s a new Error so I guess we are going in the right direction

Jul 16, 02:39:00 PM: 08970f0f INFO [POST] /.netlify/functions/_api_test-handler (API)Jul 16, 02:39:00 PM: 08970f0f Duration: 4.29 ms Memory Usage: 139 MB Jul 16, 02:39:01 PM: 2b6758db ERROR MissingStaticPage [Error]: Failed to load static file for page: /404 ENOENT: no such file or directory, open ā€˜/var/task/.next/server/pages/404.htmlā€™
at /var/task/node_modules/next/dist/server/require.js:153:19
at async loadComponentsImpl (/var/task/node_modules/next/dist/server/load-components.js:68:26)
at async NetlifyNextServer.findPageComponentsImpl (/var/task/node_modules/next/dist/server/next-server.js:762:36)
at async NetlifyNextServer.renderErrorToResponseImpl (/var/task/node_modules/next/dist/server/base-server.js:1477:30)
at async NetlifyNextServer.pipeImpl (/var/task/node_modules/next/dist/server/base-server.js:628:25)
at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:1142:21)
at async Router.execute (/var/task/node_modules/next/dist/server/router.js:315:32)
at async NetlifyNextServer.runImpl (/var/task/node_modules/next/dist/server/base-server.js:602:29)
at async NetlifyNextServer.handleRequestImpl (/var/task/node_modules/next/dist/server/base-server.js:533:20)

Thanks for trying that. This could be potentially related to a report that we received a few weeks ago that mentioned Scheduled Functions are not working with Next.js 13. In your case, they appear to be triggering, but throwing an error (as opposed to the other user in whose case they werenā€™t triggering at all).

Iā€™ve added this issue to the list as well, however based on various priorities, it might not be looked at any time soon. You may however use Netlify Scheduled Functions directly.

1 Like