amplify-js: Installing @aws-amplify/core adds node globals, which can make TS ignore potential errors

Before opening, please confirm:

JavaScript Framework

Not applicable

Amplify APIs

Not applicable

Amplify Categories

Not applicable

Environment information

# Put output below this line
System:
    OS: Linux 5.0 undefined
    CPU: (6) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.6.10 - /usr/local/bin/pnpm
  npmPackages:
    @aws-amplify/core: ^5.8.0 => 5.8.0 
    @aws-amplify/core/internals/aws-client-utils:  undefined ()
    @aws-amplify/core/internals/aws-client-utils/composers:  undefined ()
    @aws-amplify/core/internals/aws-clients/pinpoint:  undefined ()
    typescript: 5.1.6 => 5.1.6 

Describe the bug

Installing @aws-amplify/core (or one of its dependends) adds NodeJS globals, which can make TS ignore potential errors.

The @aws-amplify/core package has the "@types/node-fetch" dependency in the "dependencies" list in its package.json. This is causing TS to think that NodeJS globals (eg. process) are available, even though we are developing for a browser environment.

Among NodeJS globals there are also methods like String.at, or Array.at, which are not supported in ES2020 for example. As you can see in the Stackblitz containers linked below, this can cause TS to miss some errors, since it has its target set as ES2020 in tsconfig.json.

Solution: "@types/node-fetch" and any other package related to types, react-native, or other devtools should be included in "devDependencies", instead of "dependencies" (explanation: https://docs.npmjs.com/cli/v9/configuring-npm/package-json#dependencies - “Please do not put test harnesses or transpilers or other “development” time tools in your dependencies object.”, “If someone is planning on downloading and using your module in their program, then they probably don’t want or need to download and build the external test or documentation framework that you use.”).

I think this is important to solve, because there are a lot of users potentially affected by this. Vite for example sets its default target as ES2020 (roughly).

Expected behavior

TS should not be missing errors.

Reproduction steps

  1. Go to https://stackblitz.com/edit/vitejs-vite-krwz8t?file=package.json,src%2Fmain.ts&terminal=dev
  2. Run npx tsc in the terminal
  3. There should be 3 errors, but there are none.
  4. Go here, where @aws-amplify/core is not installed: https://stackblitz.com/edit/vitejs-vite-knsnqf?file=package.json,src%2Fmain.ts&terminal=dev
  5. Run npx tsc in the terminal
  6. You can see that 3 errors are found

You can also hover over process, at in the main.ts file to see how TS sees them (as NodeJS globals, or as missing).

Code Snippet

// Put your code below this line.

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

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @bogdanailincaipnt

Thank you for providing the detailed Stackblitz reproduction. Techinically the tsconfig.base.json shouldn’t impact the output types, they are only useful for the library maintainers. But I see the problem from the sandbox. We’ll investigate it and keep you post on this thread.

Whoops, I confused this with another @aws-sdk issue that was resolved. My apologies, @bogdanailincaipnt. This issue is not resolved and we will reopen.

@bogdanailincaipnt Thanks for the quick response and appreciated the deep dive 😃 the listed packages in the screenshot are all from @aws-sdk packages (repo) which amplify takes dependency on. I think it needs to be addressed by them so we could stop seeing this i guess. I did find one instance in Amplify Predictions, I will try to remove that and test it (I think, it should have been tree-shaken already though)

Nice! I tested with "@aws-amplify/core": "6.0.1-next.a1ea0f2.0" and it looks like it’s fixed. You can check here. The TS errors are showing when running npx tsc, which is good.

The developer preview for v6 of Amplify has officially been released with improvements to TypeScript support and much more! Please check out our announcement and updated documentation to see what has changed.

This issue should be resolved within the dev preview and upcoming General Availability for Amplify v6, but let us know with a comment if there are further issues.

Hello, @bogdanailincaipnt 👋 and thank you for opening this issue. We are currently looking into this and potentially addressing it with a fix soon. We’ll keep you updated on the progress, but appreciate your detailed findings and reproduction steps!