What is the recommended folder structure

I want to have multiple services and create the folder structure as ,

My folder structure is,

serverless
 -.serverless
 -- aws-nodejs.zip
 -- cloudformation-template-....
 - node_modules
 - users
 --.serverless
 ---cloudformation-...
 -- .nmpignore
 -- handle.js
 -- serverless.yml
 - .npmignore
 - package.json

In my handler.js , it seems ‘require’ cannot find the module ‘colors’. Is it because the node_modules is in the top folder? What is the best practice?

'use strict';

var colors = require('colors');

module.exports.hello = (event, context, callback) => {
  //console.log('hello'.green); // outputs green text
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };

  callback(null, response);

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

Is it because the node_modules is in the top folder? What is the best practice?

It works ok the node_modules is in the top folder. There may be other causes.

I think the folder structure of multiple services looks like

serverless-projects
  - project1
  -- handler.js
  -- serverless.yml
  - project2
  -- handler.js
  -- serverless.yml
  - package.json
  - node_modules

The following repository has examples of a serverless project. I think that it is good to try it also here.

or is it better to put it into each individual services, as we need to optimize the zipped file size to be uploaded to lambda?

1 Like

or is it better to put it into each individual services, as we need to optimize the zipped file size to be uploaded to lambda?

Yes, I think so.