How to work with bucklescript requires and google functions - require

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.

Related

How to enable decorators in NodeJs lambda function that are built and deployed using AWS CDK?

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.

How can I use the node.js v8 module in a react app?

I'm trying to use the serialization API from the node.js v8 module in my react app (created with create-react-app) but it doesn't seem to work.
According to the documentation it should just be a case of importing/requiring the module. When I try this, it all appears to be working as expected - no errors. I can even access methods like .serialize() and .deserialize() on the v8 object too - great. But when I try to actually run my project (using react-scripts start) I get a compilation error:
Module not found: Can't resolve 'v8' in '...'
Is it looking for a file called "v8.js" to import rather than using the node module for some reason? How do I get around this?
node_modules is only a concept when working within the node ecosystem. So it is only possible to import "v8" when within a node process.
Since you ask about a "react app" that seems to imply that you are writing something for the browser. Which now has modules which use import/export (similar to require/module.exports from node), however, that still doesn't mean that the "v8" package will be present.
Many of node's packages are C++ backed (or to use the technical term, they are "native packages") instead of being purely written in JS. Also, it should be noted that unlike dependencies listed in your "package.json" file, none of the node packages are actually downloaded when you run npm install since they are all bundled with your installation of node.

Debugging AWS Lambda node.js ES6

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.

How to create Azure Functions in 2 different language(C#, node.js) under the same project vs 2017?

I am struggling to find the way to have 2 Azure functions in different language(C# and node.js) in Azure functions Project vs 2017.
Is there a way to do that?
Thanks!
Update
This method is invalid for v2 function(runtime ~2) because now it's required to use single language in one function app. See announcement.
So, the 2 project doesn't replace the functions created by first project?
Do you mean that you found some functions are replaced after new project being deployed to Azure?
Normally they won't be replaced unless functions in new project has same function names as ones already deployed.
create Azure Functions in 2 different language(C#, node.js) under one VS project
#Mikhail is right, VS doesn't provide ways for using js Azure function. No template and debug support for now.
But if you just want to run and deploy them together using VS, with no need to debug nodejs function, there's a workaround.
Generally speaking, you have to manually add nodejs function folder structure to VS project as below.
A folder named after your js function includes function.json and xxx.js file. If you have some packages installed by npm, also add package.json and node_modules folder.
function.json, xxx.js and package.json should be set as Copy if newer, so that they can be included in output dir like c# complied dll.
Not need to include node_modules locally, function host will locate them automatically. While after deploying to azure, you need to visit kudu(https://{functionappname}.scm.azurewebsites.net/DebugConsole). Execute npm install in console under wwwroot folder to install packages in package.json.
And some function templates if needed.

Serverless Node.js Project Structure

I am building a RESTful API with the serverless framework to run on AWS (API Gateway + Lambda + DynamoDB). It's my first Node project and my first serverless production project and I have no idea how to structure it. So far I have the following
|--Functions
|-----Function1
|--------InternalModule
|-----Function2
|-----Function3
|--------InternalModule
|-----Function4
|--Shared
|-----Module1
|-----Module2
|-----Module3
|--Tests
|-----Functions
|--------Function1
|-----------InternalModule
|--------Function2
|-----------InternalModule
|--------Function3
|-----------InternalModule
|--------Function4
|-----------InternalModule
|-----Modules
|--------Module1
|-----------InternalModule
|--------Module2
|-----------InternalModule
|--------Module3
|-----------InternalModule
I keep my API endpoints (Lambda handlers) in Functions. Some of them have internal modules which they only use and there are some who use modules from Shared. I want to have unit tests for all my modules - inner and shared as well as API testing on the lambda functions. I am using mocha and chai and want to integrate everything in a pipeline which on a git push runs the linters and tests and if they are successful deploy the API to the appropriate stage. The problem is that in order to test each module I have to have chai as a local node module in every folder where I have a test file and reference the modules to be tested by a relative path. In most cases it looks really ugly because of the nesting. If I want to test an internal module from
Tests/Functions/Function1/InternalModule
and I require it on top of the test like so
require('../../../../Tests/Functions/Function1/InternalModule')
+ I have to install chai in every folder so it's reachable. The same goes for mocha and all the dependencies needed for the test and I haven't even mentioned configuration. The main idea I am now considering is weather or not I should bring all modules to a folder called modules and require them when needed - worst case
from Functions/Function1
require('../../Modules/Module1')
Also keep the test files in the module folder and run them inside, but that would require the assertion library installed in every folder. I've read about npm link and symlinks but I want to keep track of what dependencies each folder has so I can install them on the CI environment after the clean project is downloaded from GitHub where I can't do links (or I've got the whole concept wrong?)
If anyone can suggest a better solution I would highly appreciate it!
the way Node uses require is so much more than I thought!
First, Node.js looks to see if the given module is a core module - Node.js comes with many modules compiled directly into the executable binary (ex. http, fs, sys, events, path, etc.). These core modules will always take precedence in the loading algorithm.
If the given module is not a core module, Node.js will then begin to search for a directory named, "node_modules". It will start in the current directory (relative to the currently-executing Javascript file in Node) and then work its way up the folder hierarchy, checking each level for a node_modules folder.
read more here
I will try out Putting all modules in a separate folder each with it's own Folder prefixed with FunctionName_ so I know where each module is used, and the test file + package.json file. Then if I need a module I can require it from the functions with shallow nesting which would look not so bad:
from Functions/Function1
require('module-1');
with package.json
"dependencies":{
"module-1":"file:../../Modules/Function1_Module1"
}
and have a separate folder Shared where I keep the shared Modules.
I am still open for better ideas!

Resources