I am trying to debug an AWS Lambda using lambda-local but the problem is that it doesn't recognize imports and exports in node.js.
Is there a workaround other than back-refactoring my entire API to requires?
Imports and exports are not yet supported in Node.
You have two options:
1. Change your imports to use require. Or,
2. Use babel to transpile your code before deploying to Lambda.
If you use serverless-framework, the serverless-webpack plugin will be of big help to you.
Related
I have refactored my lambda function by applying DI principles using the tsyringe library. Unfortunately, after my lamdas are built to the cloud, services have not been injected properly and dependencies are undefined.
The issue is probably connected to esBuild because I could replicate this issue locally if I used it for code compilation. If I compile this code locally using TSC this issue does not occur and dependencies are properly injected.
How can I tinker the build process so I can still use AWS CDK and dependencies will be injected?
It looks like tsyringe requires the emitDecoratorMetadata option, which isn't supported natively by esbuild.
You have a couple of options to make it work:
Set the preCompilation option to true. This will run your build with tsc instead of esbuild. It'll be a bit slower, but you'll be able to emit the decorators.
Use a plugin like esbuild-plugin-tsc.
For Lambda, option #1 here is probably fine.
I need a easy way for preparing the environment for import x instead of require(x) in the express project.
I found only methods where I can do it manually, but I think this is not the best way that can be.
Is there any npm package or GitHub template that prepares the environment for this?
You can use .mjs file instead of .js file.
Documentation can be found here.
Or specify that you want to use module as the documentation shows
But for scallable application, you should use well structured code.
For example using, NestJS which is built with express out of the box. (It requires a minimal Typescript understand)
I love using optional and optional chaining feature in Node 14 - however, most of the serverless platforms (I want to avoid running an instance) support 12.x and below - what is the best and the cleanest way to go about doing this.
I believe Babel is more directed towards using for the frontend js - do let me know if there is a better way of doing it in the backend as well.
Babel is the best way. I use it for the same purpose, AWS Lambda has support for Node.js 12.x runtime only.
So transpile your code using babel, and then use serverless stuff.
I am writing AWS Lambda functions for my project (node) and now trying to figure out the best way to deploy these. Ideally I would want to
Convert the ES7 code into a version lambda can understand possibly using webpack.
Deploy using a command line tool or some npm package.
What is the best way to get this done?
You can use serverless and serverless-webpack to do both of those.
serverless is a deployment and configuration tool.
serverless-webpack is a plugin for serverless that will compile your ES7 code upon deployment.
Either you canzip it and upload it or you can use automate the deployment with AWS CodePipelne. Please see here more details about automated deployment
I'm trying to deploy functions created with bucklescript to google functions but the deploy won't run without this error :
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'bs-platform/lib/js/js_json.js'
I'm using the gcloud beta functions deploy utility. My code is using the Js.Json module, which produce var Js_json = require("bs-platform/lib/js/js_json.js"); in the outputed js code. My package.json contains the bs-platform package.
Is there a way to setup bucklescript or the gcloud utility to make my code acceptable?
BuckleScript's requires are just standard CommonJS requires, and can be bundled up into a single file using a bundler like webpack. You can also configure bsb to emit es6 modules (see the package-specs property of the bsconfig.json schema) and bundle them up using rollup.