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.45k stars 2.13k forks source link

aws-amplify with vite.config.js Can't find variable: global #10698

Closed rapgithub closed 2 years ago

rapgithub commented 2 years ago

Before opening, please confirm:

JavaScript Framework

Not applicable

Amplify APIs

REST API

Amplify Categories

auth

Environment information

``` # Put output below this line System: OS: macOS 11.7 CPU: (4) x64 Intel(R) Core(TM) i5-4308U CPU @ 2.80GHz Memory: 54.73 MB / 8.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 17.9.1 - /usr/local/bin/node npm: 8.11.0 - /usr/local/bin/npm Browsers: Chrome: 107.0.5304.110 Safari: 16.0 npmPackages: @aws-amplify/auth: ^5.0.3 => 5.0.3 @aws-sdk/eventstream-codec: ^3.212.0 => 3.212.0 (3.186.0) @aws-sdk/util-base64: ^3.208.0 => 3.208.0 @rollup/plugin-json: ^5.0.1 => 5.0.1 @rollup/plugin-node-resolve: ^15.0.1 => 15.0.1 amazon-cognito-identity-js: ^6.0.1 => 6.0.1 aws-amplify: ^5.0.3 => 5.0.3 aws-sdk: ^2.1258.0 => 2.1258.0 cpy-cli: ^4.2.0 => 4.2.0 cross-env: ^7.0.3 => 7.0.3 crypto-js: ^4.1.1 => 4.1.1 dom7: ^4.0.4 => 4.0.4 framework7: ^7.0.8 => 7.0.9 framework7-icons: ^5.0.5 => 5.0.5 framework7-icons-react: undefined () framework7-icons-svelte: undefined () framework7-icons-vue: undefined () i18next: ^22.0.6 => 22.0.6 i18next-http-backend: ^2.0.1 => 2.0.1 jwt-decode: ^3.1.2 => 3.1.2 jwt-simple: ^0.5.6 => 0.5.6 material-icons: ^1.12.1 => 1.12.2 postcss-preset-env: ^7.8.3 => 7.8.3 rater-js: ^1.0.1 => 1.0.1 rollup: ^3.3.0 => 3.3.0 (2.79.1) rollup-plugin-framework7: ^1.2.1 => 1.2.1 sass: ^1.56.1 => 1.56.1 skeleton-elements: ^4.0.1 => 4.0.1 skeleton-elements/angular: 0.0.1 snazzy-info-window: ^1.1.1 => 1.1.1 swiper: ^8.4.4 => 8.4.5 swiper_angular: 0.0.1 tocktimer: ^1.1.1 => 1.1.1 uuid: ^9.0.0 => 9.0.0 (3.4.0, 3.3.2, 8.3.2, 8.0.0) vite: ^3.2.4 => 3.2.4 vite-plugin-html: ^3.2.0 => 3.2.0 npmGlobalPackages: @angular/cli: 13.3.5 @aws-amplify/cli: 10.5.0 @vue/cli: 5.0.8 aws-amplify: 4.3.42 cordova-check-plugins: 4.0.5 cordova-plugin-update: 0.1.0 cordova-res: 0.15.4 cordova: 11.0.0 corepack: 0.10.0 framework7-cli: 7.0.0 ios-deploy: 1.11.4 npm: 8.11.0 ```

Describe the bug

Using aws-amplify with my app project to connect to aws services like cognito and I have upgraded cordova version 11 and all libraries and modules and using

import { Amplify, API, graphqlOperation } from "aws-amplify";
import aws_exports from './../../src/aws-exports';
Amplify.configure(aws_exports);

gives me now this error:

ReferenceError: Can't find variable: global

cordova version 11 uses now vite-config.js

How can I solve this using vite with amplify?

my package.json show this

  "dependencies": {
    "@aws-amplify/auth": "^5.0.3",
    "@aws-sdk/eventstream-codec": "^3.212.0",
    "@aws-sdk/util-base64": "^3.208.0",
    "@rollup/plugin-json": "^5.0.1",
    "@rollup/plugin-node-resolve": "^15.0.1",
    "amazon-cognito-identity-js": "^6.0.1",
    "aws-amplify": "^5.0.3",
    "aws-sdk": "^2.1258.0",
    "crypto-js": "^4.1.1",
    "dom7": "^4.0.4",
    "framework7": "^7.0.8",
    "framework7-icons": "^5.0.5",
    "i18next": "^22.0.6",
    "i18next-http-backend": "^2.0.1",
    "jwt-decode": "^3.1.2",
    "jwt-simple": "^0.5.6",
    "material-icons": "^1.12.1",
    "rater-js": "^1.0.1",
    "skeleton-elements": "^4.0.1",
    "snazzy-info-window": "^1.1.1",
    "swiper": "^8.4.4",
    "tocktimer": "^1.1.1",
    "uuid": "^9.0.0"
  },

any clues how to fix this issue or is it a bug? thanks

Expected behavior

no errors when using amplify and not getting ReferenceError: Can't find variable: global all the time!

Reproduction steps

npm start

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

iPhone 11

Mobile Operating System

iOS last version

Mobile Browser

browser

Mobile Browser Version

android 11

Additional information and screenshots

-

rapgithub commented 2 years ago

I found a simple solution for those who seek and has the same problem!

Just add this line to your index.html at the bottom!

  <script> var global = global || window; var Buffer = Buffer || []; var process = process || { env: { DEBUG: undefined }, version: [] }; </script>