Running eleventy build (via npm run) as AWS Lambda function - node.js

I have a eleventy Node project, which renders HTML from a JSON file.
Currently, I run this locally using npm run (which runs the eleventy CLI)
Here's the workflow I have in my head:
put the JSON file in a S3 bucket
on each file change, run the HTML build
push the output to a different S3 bucket, which serves the web page
Conceptually, I feel like this would be a standard FaaS use case.
Practically, I stumble over the fact that the Node.js-Lambda runtime always expects an explicit function handler to be invoked. It seems Eleventy does not provide a standard way to be invoked from code (or I have not discovered this yet).
I found that I could build my package into a Docker container and run the npm run as Entrypoint. This would surely work, but seems unnecessary, since the Lambda-provided Node.js runtimes should be capable of running my npm build command if I put my packages in the deployment artifact.
Do I have a knot in my brain? Anything I'm overlooking?
Would be happy about any input.

I'm not sure this is supported as I don't see it documented, but I looked at the unit tests for Eleventy and saw many examples of this (https://github.com/11ty/eleventy/tree/master/test). I tried the following and it worked. Note that init an write are both async and I do NOT properly await them, I was just trying to get a simple example:
const Eleventy = require('#11ty/eleventy');
const elev = new Eleventy('./input', './output');
elev.init();
elev.write();
console.log('done');

Related

Using music21j in node js, not in browser

I'm attempting to run music21j in node js ( repo link , npm link ).
I get ReferenceError: self is not defined
When trying to simply initialize music21j:
const music21 = require('music21j');
const n = new music21.note.Note('F#');
Does this mean it's not possible to run outside of the browser, or am I somehow initializing to the wrong environment?
The github documentation states
...or use it in your javascript/typescript project
, hence my confusion.
From the github repo:
At present it's not possible to run outside of the browser. :-( But we're working on removing certain JQuery patches that should make it easier to do. You may however need to use ES6-style imports.

How to import the googleapis npm package into meteor

I'm using the meteor framework, and trying to use google sheets to visualize some of my app's data. But for whatever reason, I can't seem to get the google api to load. I started with the command npm i googleapis and adding const {google} = require('googleapis') Which is to my knowledge correct. However, when I console.log(google) it spits out undefined.
I'm running Meteor 1.8.1, Node 6.12.0, and googleapi ^39.2.0
I would love to know what I'm doing wrong, most other people with this issue needed to update various other packages, so I've already run npm update To no avail, I've tried using import { google } from 'googleapis' Also to no avail, I saw somewhere that that would require transformations or something, but it still spits out undefined
I noticed that if I run require('googleapis') from my meteor shell, then it returns all the methods and whatnot that it's supposed to.
Ok, how I do it, to prevent adding another NPM to my Meteor projects ...
In my main.html I have this line:
<link rel="dns-prefetch" href="//maps.googleapis.com">
The next example shows adding google maps. I first make sure that, in a session, I load this library maximum once.
I call this code in multiple pages as I need the API. Since I use react, I do i in 'componentDidMount'.
if (!(window.google && window.google.maps)) {
const script = document.createElement('script')
script.src = 'https://maps.googleapis.com/maps/api/js?key=xxxxxxxxx&libraries=places'
script.defer = true
document.head.appendChild(script)
}
Removing my node_modules and running npm i did the trick

AWS Lambda function to connect to a Postgresql database

Does anyone know how I can connect to a PostgreSQL database through an AWS Lambda function. I searched it up online but I couldn't find anything about it. If you could tell me how to go about it that would be great.
If you can find something wrong with my code (node.js) that would be great otherwise can you tell me how to go about it?
exports.handler = (event, context, callback) => {
"use strict"
const pg = require('pg');
const connectionStr =
"postgres://username:password#host:port/db_name";
var client = new pg.Client(connectionStr);
client.connect(function(err){
if(err) {
callback(err)
}
callback(null, 'Connection established');
});
context.callbackWaitsForEmptyEventLoop = false;
};
The code throws an error:
cannot find module 'pg'
I wrote it directly on AWS Lambda and didn't upload anything if that makes a difference.
I wrote it directly on AWS Lambda and didn't upload anything if that makes a difference.
Yes this makes the difference! Lambda doesnt provide 3rd party libraries out of the box. As soon as you have a dependency on a 3rd party library you need to zip and upload your Lambda code manually or with the use of the API.
Fore more informations: Lambda Execution Environment and Available Libraries
You need to refer Creating a Deployment Package (Node.js)
Simple scenario – If your custom code requires only the AWS SDK library, then you can use the inline editor in the AWS Lambda console. Using the console, you can edit and upload your code to AWS Lambda. The console will zip up your code with the relevant configuration information into a deployment package that the Lambda service can run.
and
Advanced scenario – If you are writing code that uses other resources, such as a graphics library for image processing, or you want to use the AWS CLI instead of the console, you need to first create the Lambda function deployment package, and then use the console or the CLI to upload the package.
Your case like mine falls under Advanced scenario. So we need to create a deployment package and then upload it. Here what I did -
mkdir deployment
cd deployment
vi index.js
write your lambda code in this file. Make sure your handler name is index.handler when you create it.
npm install pg
You should see node_modules directory created in deployment directory which has multiple modules in it
Package the deployment directory into a zip file and upload to Lambda.
You should be good then
NOTE : npm install will install node modules in same directory under node_modules directory unless it sees a node_module directory in parent directory. To be same first do npm init followed by npm install to ensure modules are installed in same directory for deployment.

Meteor.js: How do you require or link one javascript file in another on the client and the server?

1) In node on the backend to link one javascript file to another we use the require statement and module.exports.
This allows us to create modules of code and link them together.
How do the same thing in Meteor?
2) On the front end, in Meteor is I want to access a code from another front end javascript file, I have to use globals. Is there a better way to do this, so I can require one javascript file in another file? I think something like browserify does this but I am not sure how to integrate this with Meteor.
Basically if on the client I have one file
browserifyTest.coffee
test = () ->
alert 'Hello'
I want to be able to access this test function in another file
test.coffee
Template.profileEdit.rendered = ->
$ ->
setPaddingIfMenuOpen()
test()
How can I do this in Meteor without using globals?
Meteor wraps all the code in a module (function(){...your code...})() for every file we create. If you want to export something out of your js file (module), make it a global. i.e don't use var with the variable name you want to export and it'll be accessible in all files which get included after this module. Keep in mind the order in which meteor includes js files http://docs.meteor.com/#structuringyourapp
I don't think you can do this without using globals. Meteor wraps code in js files in SEF (self executing function) expressions, and exports api is available for packages only. What problem do you exactly have with globals? I've worked with fairly large Meteor projects and while using a global object to keep my global helpers namespaces, I never had any issues with this approach of accessing functions/data from one file in other files.
You can use a local package, which is just like a normal Meteor package but used only in your app.
If the package proves to be useful in other apps, you may even publish it on atmosphere.
I suggest you read the WIP section "Writing Packages" of the Meteor docs, but expect breaking changes in coming weeks as Meteor 0.9 will include the final Package API, which is going to be slightly different.
http://docs.meteor.com/#writingpackages
Basically, you need to create a package directory (my-package) and put it under /packages.
Then you need a package description file which needs to be named package.js at the root of your package.
/packages/my-package/package.js
Package.describe({
summary:"Provides test"
});
Package.on_use(function(api){
api.use(["underscore","jquery"],"client");
api.add_files("client/lib/test.js","client");
// api.export is what you've been looking for all along !
api.export("Test","client");
});
Usually I try to mimic the Meteor application structure in my package so that's why I'd put test.js under my-package/client/lib/test.js : it's a utility function residing in the client.
/packages/my-package/client/lib/test.js
Test={
test:function(){
alert("Hello !");
}
};
Another package convention is to declare a package-global object containing everything public and then exporting this single object so the app can access it.
The variables you export NEED to be package-global so don't forget to remove the var keyword when declaring them : package scope is just like regular meteor app scope.
Last but not least, don't forget to meteor add your package :
meteor add my-package
And you will be able to use Test.test in the client without polluting the global namespace.
EDIT due to second question posted in the comments.
Suppose now you want to use NPM modules in your package.
I'll use momentjs as an example because it's simple yet interesting enough.
First you need to call Npm.depends in package.js, we'll depend on the latest version of momentjs :
/packages/my-moment-package/package.js
Package.describe({
summary:"Yet another moment packaged for Meteor"
});
Npm.depends({
"moment":"2.7.0"
});
Package.on_use(function(api){
api.add_files("server/lib/moment.js");
api.export("moment","server");
});
Then you can use Npm.require in your server side code just like this :
/packages/my-moment-package/server/moment.js
moment=Npm.require("moment");
A real moment package would also export moment in the client by loading the client side version of momentjs.
You can use the atmosphere npm package http://atmospherejs.com/package/npm which lets you use directly NPM packages in your server code without the need of wrapping them in a Meteor package first.
Of course if a specific NPM package has been converted to Meteor and is well supported on atmosphere you should use it.

sendgrid make test throws errors

I'm running node on my vps server. node is at my root, my app.js with node_modules (express, socket.io) are in /home/vps/public_html/
when following github readme for setting up sendgrid i run into trouble:
config.js is not live by the sounds of it as when I run sendgrids simple code example if i
a.) refer to config.js by doing:
var sendgrid=new SendGrid(user,key);
node kills its self because user is not defined.
b.) bypass config.js by doing:
var sendgrid=new SendGrid({user:'my_user_name',key:'my_password'});
I get console.log(message) of [ 'Permission denied, wrong credentials' ]
here is another image this one is of my public_html structure:
does any one know how to activate this config.js?
Should I have installed node.js into my public_html in the beginning?
So, there are a few things wrong here:
1) We have a typo in the README (which has now been updated)
tl;dr - you should type npm test not make test
Essentially, you're typing make test and make is coming back and saying "Hey, there's no rule for test in the cwd. It doesn't look like there's anything for me to do. Bye!". If you look carefully, there is no Makefile in the node library, so there's obviously not going to be any rules. So make definitely won't work in this case. What the README should have said is npm test. NPM is the package manager for node and it has a helper method test which runs all the tests for a given module.
Also, just to be clear - when you're typing npm test, all you are doing is running the tests for the library. Really this should only be necessary if you're adding features or fixing bugs on the library itself. If you're trying to use the library to send email, you should read the section titled "Usage".
2) You have a typo in your code (which is why the other sample didn't work)
Your code looks like this:
var sendgrid = new SendGrid({user:'my_user_name', key:'my_password'});
The code sample that we provide looks like this:
var sendgrid = new SendGrid(user, key);
Notice the difference? You're passing in a javascript object and we're expecting two discrete string values instead. The library is interpreting that as your username is "{user:'my_user_name', key:'my_password'}" with no password (because you didn't provide a second parameter). Instead you should do the following:
var sendgrid = new SendGrid("my_user_name", "my_password");

Resources