Javascript - Cannot initialize a AWS Textract object with AWS-SDK

Viewed 2458

I want to use the textract API for document analysis, but when I tried to create an instance of AWS.Textract it throws an error saying

module initialization error: TypeError

I tried different things, Initially I tries this,

const AWS = require('aws-sdk');
const Textract = new AWS.Textract();

it didn't work and said cannot find object

then after some googling, I found this link and adjusted the code to this

require('aws-sdk/clients/textract');
var textractClient = new AWS.Textract();

and this,

const Textract = require('aws-sdk/clients/textract');
var textractClient = new Textract();

None of these are working, what am doing wrong here?

1 Answers

Don't take offence but your title is bit confusing because it refers to aws-cli however the code snippet in your description is in nodejs. Can you kindly clarify which is which?

In JavaScript/nodejs SDK you can initialize Amazon Textract object as illustrated by the following code snippet :

'use strict';

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');

// Set region
AWS.config.region = 'us-east-1';

var textract = new AWS.Textract();

// Set parameters for the API
var params = {
    DocumentLocation: { /* required */
      S3Object: {
        Bucket: 'syumaK-bucket',
        Name: 'document.pdf'
      }
    },
    FeatureTypes: [ /* required */
      'TABLES','FORMS'
    ],
    NotificationChannel: {
      RoleArn: 'arn:aws:iam::19250632xxxx:role/AWSTextractRole', /* required */
      SNSTopicArn: 'arn:aws:sns:us-east-1:19250632xxxx:AmazonTextractTopic1562662993926'/* required */
    }
  };
  textract.startDocumentAnalysis(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  });

I have tested the above code snippet using the following environment spec:

  • OS : Mac High Sierra v10.13.6
  • aws-sdk: "^2.489.0"
Related