aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

issue with aws service discovery javascript

Open Ravi-Rsankar opened this issue 3 years ago • 12 comments

Im developing a nodejs application that needs to register to the AWS service discovery on the app start. I'm using the @aws-sdk/client-servicediscovery lib in my node application. As a reference, I am using the code from here: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-servicediscovery/index.html.

const { ServiceDiscoveryClient, CreateHttpNamespaceCommand } = require("@aws-sdk/client-servicediscovery");
const client = new ServiceDiscoveryClient({ region: "xxx" });
const params = {
    "ServiceId":"xxx",
    "InstanceId":"xxx",
    "CreatorRequestId":new Date(new Date().toUTCString()),
    "Attributes": {
        AWS_INSTANCE_IPV4: "xxx.xx.xx.xx",
        AWS_INSTANCE_PORT: xxx,
        service: "xxx",
      }
};

const command = new CreateHttpNamespaceCommand(params);
client.send(command).then(
    (data) => {
      console.log(data)
    },
    (error) => {
        console.log(error)
    }
  );

While running the app, getting these errors.

/application/node_modules/@aws-sdk/shared-ini-file-loader/dist-cjs/slurpFile.js:5
const { readFile } = fs_1.promises;
        ^

TypeError: Cannot destructure property `readFile` of 'undefined' or 'null'.
    at Object.<anonymous> (/application/node_modules/@aws-sdk/shared-ini-file-loader/dist-cjs/slurpFile.js:5:27)
    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> (/application/node_modules/@aws-sdk/shared-ini-file-loader/dist-cjs/loadSharedConfigFiles.js:8:21)
    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)

I am new to the AWS SDK so need some help registering my service to the service discovery. Thanks in advance!

Ravi-Rsankar avatar Apr 15 '22 09:04 Ravi-Rsankar

@Ravi-Rsankar thanks for opening the issue, transferring it to the correct repo. https://github.com/aws/aws-sdk-js-v3

ajredniwja avatar Apr 15 '22 22:04 ajredniwja

Same issue here when bumping @aws-sdk packages from 3.36.x to latest.... Do you have any updates. please?

khitrenovich avatar May 13 '22 12:05 khitrenovich

Same here. Anyone found a solution?

brunobasto avatar May 19 '22 19:05 brunobasto

Same issue here when bumping @aws-sdk packages from 3.36.x to latest.... Do you have any updates. please?

Update (May 23, 2022) - Bump to v3.95 across the board of all the services we are using brings no changes.

    TypeError: Cannot destructure property 'readFile' of 'fs_1.promises' as it is undefined.

      at Object.<anonymous> (../../node_modules/@aws-sdk/shared-ini-file-loader/dist-cjs/getSSOTokenFromFile.js:6:9)
      at Object.<anonymous> (../../node_modules/@aws-sdk/shared-ini-file-loader/dist-cjs/index.js:7:22)

image

khitrenovich avatar May 23 '22 13:05 khitrenovich

@ajredniwja My apologies for tagging you directly here. I'm desperately looking for any updates about the issue, since currently we are stuck at a really old version of AWS SDK and cannot upgrade because of the build failures.

Also, had anybody here found any workaround for that?

khitrenovich avatar Jun 16 '22 21:06 khitrenovich

Facing the same issue with @aws-sdk/client-s3 on AWS CodeBuild with nodejs v14. Local builds with nodejs v14.15.x still working.

Bildschirmfoto 2022-07-07 um 15 17 09

mbunge avatar Jul 07 '22 13:07 mbunge

@mbunge Did you find a solution to this?

dopedwolf avatar Jul 12 '22 20:07 dopedwolf

This is possibly related to https://github.com/aws/aws-sdk-js-v3/issues/3828

ajredniwja avatar Aug 08 '22 15:08 ajredniwja

Any updates?

jasonterando avatar Jan 27 '23 04:01 jasonterando

Hi guys, I'm using two AWS packages:

"@aws-sdk/client-secrets-manager": "^3.282.0"
"@aws-sdk/client-ssm": "^3.282.0"

Node.JS version 16.19.1 and I'm also facing this error, it's like:

  TypeError: Cannot destructure property 'writeFile' of 'fs_1.promises' as it is undefined.

      1 | const Confidence = require('confidence')
    > 2 | const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager')

I was testing a possible fix locally and I needed to change, from:

const fs_1 = require("fs");
const { writeFile } = fs_1.promises;

to (I saw it on that TODO):

const fs_1 = require("fs/promises");
const { writeFile } = fs_1;

In three files:

  • writeSSOTokenToFile
  • slurpFile
  • getSSOTokenFromFile

After it I got success on the tests that I was running.

Can anyone help, applying those changes on the SDK?

WarleyGabriel avatar Mar 06 '23 23:03 WarleyGabriel

I've just realized that I was mocking fs in my test:

jest.mock('fs')

I changed to use jest.spyOn(fs, 'writeFileSync').mockReturnValue({}), now it's working!

WarleyGabriel avatar Mar 07 '23 13:03 WarleyGabriel

You can also mock the 'fs' module and retain functionality using jest.requireActual() like this:

const fs = require('fs');

jest.mock('fs', () => ({
  ...jest.requireActual('fs'),
  openSync: jest.fn(),
  createReadStream: jest.fn(),
  writeFileSync: jest.fn(),
  writeSync: jest.fn(),
  closeSync: jest.fn()
}));

All 'fs' functionality will remain and only the listed functions will be mocked: openSync, createReadStream, writeFileSync, writeSync, closeSync.

zterlizz avatar Aug 10 '23 15:08 zterlizz