Directories are not present in Netlify Functions

I am trying to deploy a Node app on Netlify functions.

Site: https://gracious-joliot-9ce76f.netlify.app/.netlify/functions/index

Here is my functions/index.js

const startStrapi = require("strapi/lib/Strapi");
const serverless = require("serverless-http");

module.exports.handler = async (event, context) => {
  if (!global.strapi) {
    console.log("Cold starting Strapi");
    await startStrapi({ dir: __dirname }).start();
  }
  const handler = serverless(global.strapi.app);
  return handler(event, context);
};

My netlify.toml:

[build]
functions = "./functions" 
publish = "functions/build"

Directory Structure:

.
├── functions
│   ├── api
│   │   └── people
│   │       ├── config
│   │       │   └── routes.json
│   │       ├── controllers
│   │       │   └── people.js
│   │       ├── models
│   │       │   ├── people.js
│   │       │   └── people.settings.json
│   │       └── services
│   │           └── people.js
│   ├── build
│   │   ├── index.html
│   │   ├── main.aa82ad43.chunk.js
│   ├── config
│   │   ├── database.js
│   │   ├── functions
│   │   │   ├── bootstrap.js
│   │   ├── middleware.js
│   │   └── server.js
│   ├── index.js
│   ├── node_modules
│   ├── package.json
│   ├── package-lock.json
│   ├── public
│   │   ├── robots.txt
│   │   └── uploads
│   ├── README.md
└── netlify.toml

Logs:

4:42:57 PM b4f197b5 INFO   Cold starting Strapi
4:42:57 PM b4f197b5 ERROR  Invoke Error 	{"errorType":"Error","errorMessage":"Cannot find module '/var/task/src/package.json'\nRequire stack:\n- /var/task/src/node_modules/strapi/lib/core/app-configuration/index.js\n- /var/task/src/node_modules/strapi/lib/Strapi.js\n- /var/task/src/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","code":"MODULE_NOT_FOUND","requireStack":["/var/task/src/node_modules/strapi/lib/core/app-configuration/index.js","/var/task/src/node_modules/strapi/lib/Strapi.js","/var/task/src/index.js","/var/task/index.js","/var/runtime/UserFunction.js","/var/runtime/index.js"],"stack":["Error: Cannot find module '/var/task/src/package.json'","Require stack:","- /var/task/src/node_modules/strapi/lib/core/app-configuration/index.js","- /var/task/src/node_modules/strapi/lib/Strapi.js","- /var/task/src/index.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 module.exports (/var/task/src/node_modules/strapi/lib/core/app-configuration/index.js:66:19)","    at new Strapi (/var/task/src/node_modules/strapi/lib/Strapi.js:63:19)","    at module.exports (/var/task/src/node_modules/strapi/lib/Strapi.js:490:18)","    at Runtime.module.exports.handler (/var/task/src/index.js:14:11)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
4:42:57 PM b4f197b5 Duration: 29.10 ms	Memory Usage: 124 MB

I think the error occurs because not all the files and folders in functions directory are uploaded. How do I resolve this issue?

Hi @ravgeetdhillon,

I’m trying to understand what you’ve done/are trying to do here. Do you have the pages of your site inside the functions directory?

You are using Strapi along with another tool (e.g. Nuxt, Netlify CMS) to build your site?

Hi @coelmay ,

What I want to do is to make all the files and folders in the functions directory available to all the functions - in this case index.js. Right now only node_modules are uploaded, rest of the directories like api, config and files like package.json are not uploaded.

I believe what you are trying to achieve is not how lambda functions work. I shall point you to these resources:

1 Like