0
votes

I have created a serverless aws-nodejs template project and in it I have organized my js files in the following way -

project:root -
   | .env
   | src -
         | controllers -
            <js_files_here> 
         | helpers - 
            <js_files_here>
         | models -
            <js_files_here>
         | routes -
            <yml_files_here>

And this is my serverless.yml -

service: rest-api

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: ap-south-1

plugins:
  - serverless-bundle
  - serverless-offline

functions:
  ${file(./src/routes/index.yml)}

and in one of my js files I am trying to use -

require('dotenv').config({ path: './.env' });

So I am trying to load some of the environment variables from this .env file. Now this is working as expected when I test these files locally with - sls offline start but when I deploy them to a aws account, the apis stop working as expected and also when I see the package (rest-api.zip) file in the .serverless directory I do not see all the files from src directory packaged in there.

So, how do I fix this issue and deploy my project correctly with serverless on aws ?

1

1 Answers

2
votes

Your problem is that WebPack failed to include the file in its transitive closure when trying to find out all the files you need, due to dotenv importing it dynamically.

You could use a WebPack plugin to explicitly include your env file, such as this one.