It seems that Serverless is unable to recognise imports from subdirectories in Node: apolloServer.js relies on imports from subdirectories such as utils, api, graphql etc…
It would be almost impossible to have all the code in the single apolloServer.js file.
Is there a way to make Serverless recognise imports from subdirectories in Node ?
Does it work when you deploy to actual Lamba e.g. is this a bug in serverless-local ?
Have you seen the docs on the package part of serverless.yml ? You can include any files in the deployment package, though we’d usually exclude whole “src” folders, and/or copy the bits needed to a “dist” and refer to them from there; YMMV.
The “import” statement is a part of the ECMAScript module system and is not supported by default in Node.js runtime v12 and below. To use the “import” statement in a serverless environment, you need to use a transpiler such as Babel to convert your code to a format that can be understood by Node.js.
Here’s a general outline of the steps you can follow to get serverless to recognize “import” in Node.js:
Install required packages: Install the following packages using npm:
Transpile your code: Transpile your code using Babel from the command line:
npx babel src --out-dir dist
Update your serverless configuration: In your serverless.yml file, update the “handler” field to point to the transpiled code in the “dist” folder:
functions:
hello:
handler: dist/handler.default
events:
http:
path: /
method: get
Deploy your code: Deploy your serverless application as usual.
This should allow you to use the “import” statement in your Node.js code and have it work in a serverless environment. Note that this is a general outline, and you may need to make additional changes to your code or configuration depending on your specific requirements. I have detailed about it in this article you can also check for further enhancement.