How do I use a custom AWS SDK with nodejs? - node.js

I want to make a nodejs application that uses the AWS SDK. However, it only really needs to use S3, and I want the application to have a small filesize.
AWS has a custom sdk builder which should make a custom SDK with only the required packages. Unfortunately, this doesn't work, even when I select all of the services. How do I make a custom SDK that works with node?
I've tried using the npm install aws-sdk, and that works, but it's 40MB. I've also tried selecting all of the services in the service builder (only 3MB), but that doesn't work.
It always gives an error at this line:
var aws = require("./aws-sdk-2.462.0.min.js");
ReferenceError: AWS is not defined
at Object.<anonymous> (/home/cwaugh/workspaces/meta-lambda/aws-sdk-2.462.0.min.js:7:30180)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/cwaugh/workspaces/meta-lambda/handler.js:5:11)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)

That's the browser SDK. For Node you can use this module which only provides an S3 Client. That page has a clear example showing how to call putObject using this custom SDK.
There's many different individual modules for a variety of AWS services. You can find more information here.

This is now possible with AWS SDK for JavaScript v3
You can install only the required packages as follows :
npm install #aws-sdk/client-s3
Use them as :
const {S3} = require('#aws-sdk/client-s3');

Related

Electron: TypeError: Cannot create property 'default' on symbol 'Symbol(nodejs.util.inspect.custom)'

I've developed an electron app with following tech stack:
NodeJS (10.15) with Babel to allow ES6 import/export syntax.
Sequelize on SQLite and GraphQL to handle an internal database.
ReactJS (bundled) for views.
I've configured everything to work with Electron and running "electron" command in project root folder works without errors.
Now i'm trying to distribute it using this tutorial: https://electronjs.org/docs/tutorial/application-distribution
So i've simply downloaded the 4.1.4 binaries for windows and moved the project folder under resources/app/
But running the electron.exe i've the following error:
App threw an error during load
TypeError: Cannot create property 'default' on symbol 'Symbol(nodejs.util.inspect.custom)'
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\nodejsCustomInspectSymbol.js:18:84)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\nodejsCustomInspectSymbol.js:20:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Module._compile (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:722:10)
at Object.newLoader [as .js] (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:559:12)
at Function.Module._load (internal/modules/cjs/loader.js:551:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\defineToJSON.js:8:57)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\defineToJSON.js:36:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Module._compile (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:722:10)
at Object.newLoader [as .js] (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:559:12)
at Function.Module._load (internal/modules/cjs/loader.js:551:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
i've also tried to run npx electron resources/app/ from binaries folder and i've got the same error.
So i think the issue is something about the paths.
i've also tried electron-packager and the error is the same because this tool does the same process of guide above.
What can i do to fix it?
UPDATE1: after some investigations i've found that babel causes the issues. I guess when you run a project from a parent folder. I'm trying to find a solution
i've solved using this kind of configuration for babel/register:
const rootPath=path.resolve(path.join(__dirname, "../../"));
require("#babel/register")({
root: rootPath,
extends: path.join(rootPath, '.babelrc'),
ignore: [/node_modules/],
only: [rootPath],
})

Datastore is not a contructor error when trying ./bin/www

I received an error when I try to execute the command "./bin/www" in my installed ubuntu application on Windows.
Error is as below:
/mnt/c/Users/admin/Documents/html/developer_portal/app.js:91
dataset: new Datastore(datastoreOptions)
^
TypeError: Datastore is not a constructor
at Object.<anonymous> (/mnt/c/Users/admin/Documents/html/developer_portal/app.js:91:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/mnt/c/Users/admin/Documents/html/developer_portal/bin/www:7:11)
at Module._compile (module.js:652:30)
I ran into the same issue trying to re-use some older code with v3.0 of the node helper library for Datastore. It looks like the import syntax has changed a bit in v3. Line 91 in your app.js probably looks something like:
const datastore = new Datastore({projectID: 'myProject'});
Just before that you probably declare Datastore like:
const Datastore = require('#google-cloud/datastore');
... when what you now want is:
const {Datastore} = require('#google-cloud/datastore');

Serverless Node TypeScript app can't import module

I'm tired of this error! This is Serverless app with Lambda in AWS. Locally it works perfectly. Testing locally didn't work for load of other reasons. I'm testing in staging. Yet I'm getting this error:
Unable to import module 'src/engine/handler': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous>(/var/task/src/engine/controllers/image.js:43:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I followed all the steps advised elsewhere including: moving all dependencies to dependencies (out of dev), installing serverless and all. I'm using Node 6.10.3 and TypeScript 3.1. Can you help?
If you're trying host your nodejs projects which has so many dependencies not related to aws services inside, you better zip your projects in S3 first then import lambda from S3.

Cannot find librdkafka on Heroku

I am trying to get a simple Kafka consumer up and running on Heroku. I am using Node 8.11 and node-rdkafka. I have it running locally. When I try to run it on Heroku, I get this error:
Error: librdkafka++.so.1: cannot open shared object file: No such file or
directory
at Object.Module._extensions..node (module.js:681:18)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at bindings (/app/node_modules/bindings/bindings.js:81:44)
at Object.<anonymous> (/app/node_modules/node-rdkafka/librdkafka.js:10:32)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
I know the library is a wrapper for librdkafka and it looks like it just can't find it. According to the list of things preinstalled on Heroku Ubuntu instance, it looks like librdkafka-dev and librdkafka1 are installed. I don't know how to use those, or if something else is going on.
You have to use the Confluent librdkafka package.
Add this to your Aptfile:
:repo:deb [arch=amd64] http://packages.confluent.io/deb/4.0 stable main
Still having issues after doing this, but they are different issues and it looks like I'm closer.

AWS Lambda - Create Thumbnail

I am using AWS Lambda for creating resized thumbnails. The source is an S3 bucket and the destination is another S3 bucket. I was able to create the lambda function from Amazon CLI. The function is triggered whenever a new image is added to the source bucket. But, the resize/thumbnail creation is not working. I can see some issue in the CloudWatch Console.
The content of the uploaded ZIP package is:
CreateThumbnail.js, node_modules
Unable to import module 'CreateThumbnail': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/CreateThumbnail.js:2:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Best regards,
BP
Finally able to find the source of the bug. The command used to make the zip file was causing the issue.
The correct command is:
zip -r CreateThumbnail.zip *

Resources