aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

FormData Related Exception When Utilizing Node.js <16.15.0 #13155

Open EdmundasD opened 8 months ago

EdmundasD commented 8 months ago

Before opening, please confirm:

JavaScript Framework

Not applicable

Amplify APIs

REST API, GraphQL API

Amplify Version

v6

Amplify Categories

api

Backend

Amplify CLI

Environment information

``` Binaries: Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node Yarn: 1.22.19 - ~/.yarn/bin/yarn npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm Browsers: Chrome: 123.0.6312.58 Safari: 17.2.1 npmPackages: @types/cookie: ^0.6.0 => 0.6.0 @types/isomorphic-fetch: ^0.0.35 => 0.0.35 @types/node: ^16 => 16.18.38 @types/yargs: ^17.0.2 => 17.0.2 aws-amplify: ^6.0.18 => 6.0.18 cookie: ^0.6.0 => 0.6.0 dibs-prettier-config: ^2.0.0 => 2.0.0 form-data: ^4.0.0 => 4.0.0 isomorphic-fetch: ^3.0.0 => 3.0.0 prettier: ^3.2.5 => 3.2.5 typedoc: ^0.24.8 => 0.24.8 typedoc-plugin-markdown: ^3.15.3 => 3.15.3 typescript: ^5 => 5.1.6 yargs: ^17.1.1 => 17.1.1 ```

Describe the bug

According to Amplify's Set up Amplify prerequisites, the Node.js requirement is v14.x or later. However, in the codebase, there is a reference to the global FormData class, which was added to Node.js inv16.15.0, according to the [Node.js docs](https://nodejs.org/docs/latest-v16.x/api/globals.html#class-formdata:~:text=the%20fetch()%20function.-,Class%20FormData,-%23).

Expected behavior

I believe one of two things should be done...

a) The Amplify's prerequisite docs should be updated to require Node.js v16.15.0 or later

b) Include form-data as a dependency from which to reference FormData

Reproduction steps

Occurred while migrating from v5 to v6.

Full disclosure: Have not attempted to create a new project with v6 from scratch to try and reproduce the issue.

Code Snippet

{
  data: {},
  errors: [
    ReferenceError: FormData is not defined
        at transferHandler (/Users/edmundasdaunoras/Documents/Codebases/dibs-utils/dibs-amplify/node_modules/@aws-amplify/api-rest/dist/cjs/apis/common/handler.js:21:27)
        at /Users/edmundasdaunoras/Documents/Codebases/dibs-utils/dibs-amplify/node_modules/@aws-amplify/api-rest/dist/cjs/apis/common/internalPost.js:27:56
        at job (/Users/edmundasdaunoras/Documents/Codebases/dibs-utils/dibs-amplify/node_modules/@aws-amplify/api-rest/dist/cjs/utils/createCancellableOperation.js:25:19)
        at createCancellableOperation (/Users/edmundasdaunoras/Documents/Codebases/dibs-utils/dibs-amplify/node_modules/@aws-amplify/api-rest/dist/cjs/utils/createCancellableOperation.js:48:16)
        at Object.post (/Users/edmundasdaunoras/Documents/Codebases/dibs-utils/dibs-amplify/node_modules/@aws-amplify/api-rest/dist/cjs/apis/common/internalPost.js:26:68)
        at GraphQLAPIClass._graphql (/Users/edmundasdaunoras/Documents/Codebases/dibs-utils/dibs-amplify/node_modules/@aws-amplify/api-graphql/dist/cjs/internals/InternalGraphQLAPI.js:234:60)
  ]
}

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 7 months ago

@EdmundasD, was the solution to unblock you and resolve this to upgrade your version of Node.js?

Looks like you're correct that we need to update our Set Up Amplify Prerequisites sections to display the proper version of node (as seen in the v5 to v6 migration guides).

EdmundasD commented 7 months ago

Thanks for looking into this @cwomack. I have yet to try seeing if this works with an upgraded Node.js version as that currently might not be a viable solution in my specific case. But, I can certainly try it on my local machine.

cwomack commented 7 months ago

@EdmundasD, let me know upgrading to a more recent version of Node.js resolves the issue for you. Can you confirm if setting skipLibCheck to true in your ts.config under compilerOptions is another way to work around the errors? Thanks.

EdmundasD commented 7 months ago

So I played around with this and this is what I found:

chrisbonifacio commented 7 months ago

Hi @EdmundasD can you confirm what version of aws-amplify you are using so that we can reproduce the issue? It seems a while back we specifically addressed a similar issue in a PR

EdmundasD commented 7 months ago

Sure @chrisbonifacio. I'm using v6.0.18.

chrisbonifacio commented 6 months ago

Hi @EdmundasD, apologies for the delay. I had some issues downgrading to Node 14 due to the version of python installed on my machine. Tried downgrading with nvm but apparently I'd need to downgrade my version of python3 as well...

image

If you get a chance, can you try reproducing the issue on a fresh project and let us know if the issue persists?

EDIT: downgrading to 16.0.0 should also reproduce this issue but my local test projects are on Next which require Node 18.17.0+. Can you please share your package.json file so that we use the same framework and versions as your project?

Or, if you can provide a small sample app that would reproduce the issue please do 🙏

EdmundasD commented 6 months ago

@chrisbonifacio, tried with latest aws-amplify version of 6.3.1 but still encountering the same error.

Went through Amplify's docs and created a small example: https://github.com/EdmundasD/amplifyv6/tree/main

Let me know if you have any questions.

chrisbonifacio commented 5 months ago

Thank you for the repo @EdmundasD. I was able to reproduce the issue consistently. We will be updating the docs to reflect the correct node version support.