Azure Functions use import instead of require - node.js

What is the configuration requirements to use import instead of require?
I'm using function runtime v2.
I tried upgrading node to v10.12.0 but still get this error when it hits the imports
Worker was unable to load function store: 'SyntaxError: Unexpected token {'
I have node version set to 10.12.0 in local.settings and in package.json.
my function is setup like this ...
module.exports = async function(context, queueMessage) {
import { cosmos } from "#azure/cosmos";
import { updateChat } from "./channels/chat/newChatMessage";
import { updateAttributeStatus } from
"./channels/attribute/updateAttributeStatus";
import { documentRequest } from "./channels/document/documentRequest";
...
Which version of node is supported by Azure Functions and is import supported? If so, how do I set it up?
Thanks,
Donnie

According to the docs these versions are supported for V2:
Active LTS and Current Node.js versions (8.11.1 and 10.6.0
recommended). Set the version by using the
WEBSITE_NODE_DEFAULT_VERSION app setting.
So the node version has to be set in the Application Settings as explained here.
Regarding the use of import vs require, this is still an experimental feature in Node, therefore I don't think it's possible to use this in Azure Functions yet.
I'd probably go with TypeScript instead and transpile it before uploading (you can find some examples on how to get started with that on GitHub).

Related

import twilio's Authy library in nest js

we usually use below statement to use authy library in node file using js ,mostly by require statement !
const authy = require('authy')('API KEY');
I've moved my code to nest eco system and now How should i do the same using typescript ,as i also want to pass API Key to it ?
I've tried below code as well ,but still it's not working
import { authy } from 'authy'(API KEY)
suggest something !
I have faced a similar issue in my NestJS project when using twillio library.
Currently, I have resolved this by importing it this way:
import authy = require('authy');
If, this doesn't work for you (for any reason e.g. TypeScript compile error), then can you try the following import statement?
import * as Authy from 'authy';
Also, let me know which one works for you.

Conditional import/export when using different environment

I'm currently developing a Lambda function using node14 on AWS, but I have an issue when I want to develop it locally.
In my dev environment I have a corporate proxy needed to connect to AWS. In this case, I use aws-sdk-proxy library. However, I don't want to use this package while in production (as Lambda already has the aws-sdk injected in lambda core).
So, I created this snippet to perform the switch between the 2 environments:
// aws.js
import AWSPROD from 'aws-sdk'
import AWSDEV from 'aws-sdk-proxy'
import config from '../lib/Config.js'
import logger from './logger.js'
let AWS = null
if (config.get('ENVIRONMENT') === 'dev') {
logger.debug('[DEV] Using aws-sdk-proxy')
AWS = AWSDEV
} else {
AWS = AWSPROD
}
export default AWS
Not so clean, but it works. With this code, I can perform this:
import AWS from './aws.js'
Now the problem is that this code implies that I must provide aws-sdk-proxy into the "prod" dependencies of my package.json.
I think it breaks performance of the lambda as the code raised 9Mo (2Mo without), but I wish to keep this way of calling "AWS" SDK [the 2d code block].
I tried to use required or dynamic import but none of these solutions work.
Do you have any advice to improve my code?
PS:
The code is transpiled to ES5 using Babel to fit Lambda requirements
aws-sdk-proxy library must stay in devDependencies as it's a dev dependency

How to use an angular module that does not support Universal with an angular/cli app using SSR

Hey everyone I have been building an Angular app that is using Universal with SSR for a while not and every so often I would include a module that would cause the server to fail silently and never knew why, last night I figured out it was because the module I tried to include (ngx-editor) does not support Universal.
Is there a way for me to include a module such as ngx-editor that does not support Universal in my application? Or do I have to find one that supports Universal?
Thanks a lot in advance.
You could try not calling this module's components depending on the platform, e.g. by checking the platform dynamically in your code ( https://angular.io/api/common/isPlatformBrowser)
import {isPlatformBrowser} from "#angular/common";
//...
constructor(#Inject(PLATFORM_ID) private platformId: Object)
{
if(isPlatformBrowser(this.platformId))
{
//call module's methods/components...
}
else { /*server side*/ }
}
You may also need to modify your server module (app.server.module.ts) to not include modules that do not support angular-universal

How do I get a specific dependency with grab?

Im using the AWS SDK:
#Grab(group='com.amazonaws', module='aws-java-sdk-elasticbeanstalk', version='1.11.232')
import com.amazonaws.regions.Regions
import com.amazonaws.services.elasticbeanstalk.*
import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest
import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.auth.EnvironmentVariableCredentialsProvider
class testAws {
static Object test (){
def awsCreds = new BasicAWSCredentials('sdfsdfdf', 'sdfsdfdsf')
def ebClient = new AWSElasticBeanstalkClientBuilder()
.withRegion(Regions.valueOf('US-EAST-1'))
.withCredentials(new AWSStaticCredentialsProvider (awsCreds))
.build()
ebClient.describeEnvironments(
new DescribeEnvironmentsRequest().withEnvironmentNames('zzzzz')
).getEnvironments()
}
}
I get this error:
Caught: java.lang.NoSuchMethodError: `org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(Ljavax/net/ssl/SSLContext;Ljavax/net/ssl/HostnameVerifier;)V`
Doing some googling i found the aws sdk depends on a specific version of the http library. If this was a java project I would define that specific version in my pom or gradle config. How do I do this with grab?
To be clear I dont need to grab http explicitly because it gets pulled in as a dependency of the aws sdk. The problem is I have a different (newer) version installed locally. Even if I explicitly grab the older version it still throws this error. If I uninstall the new version from my system it works. But I want to enforce this in groovy and not have to modify local environemnt.

Using a Node.js class in Cloud Functions for Firebase

I am a new to Node.js and Firebase.
I have successfully tried to deploy some Cloud Functions to test them a bit.
I have a Node.js project in which I have a class defined as:
import * as Api from './api';
export default class MyClass {
constructor(props) {[...]}
someFunction(props) {
return Api.someOtherFunction(props.arg1).then([..]).catch([..]);
}
}
In the Api code I use the firebase admin SDK and I work with the real time database.Ex.:
ref.child(`users/${userId}`).set({
id: userId,
arg1: arg1,
arg2: arg2
});
Now, the problem is that I would like to use MyClass in a cloud function.
I have read a lot about ES6 in Cloud Functions, too (ex.: here), but I am not able to get rid of the error message
SyntaxError: Unexpected token import
I have tried to convert to require statements but I am not able to require my local module where MyClass is.
I don't care if it will be a Node.js local module or simply some classes in clear hierarchical structure.
What I would like to ask is if there is a specific documentation about this situation (I have searched a lot for it) and/or if I am following the right way to structure my project.
If the answer is "NO", please give me some tips on how to structure it.
Cloud Functions (node.js) currently does not support the ES6 import syntax, you have to use require().
The examples in the article you linked are using Babel to convert ES6 to ES5. Even if you convert your import statements to require() you're going to encounter the fact that ES5 does not support the class syntax. If you want to do what the article it doing, follow the steps to get Babel converting your ES6 script to ES5 before sending it to Firebase Cloud.

Resources