Amplify-js: Dont work on node because of window

Created on 30 Sep 2018  路  10Comments  路  Source: aws-amplify/amplify-js

Describe the bug
Dont work on node due to use of window obj

To Reproduce

import Auth from '@aws-amplify/auth'
import Amplify from '@aws-amplify/core'

Expected behavior
No errors
Desktop (please complete the following information):
Node 10
"@aws-amplify/auth": "^1.2.5-unstable.3",
"@aws-amplify/core": "^1.0.14-unstable.0"

Additional context

ReferenceError: window is not defined
at Object.<anonymous> (C:\Dev\PycharmProjects\dailyidea\front\node_modules\@aws-amplify\core\lib\RNComponents\index.js:24:20)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:\Dev\PycharmProjects\dailyidea\front\node_modules\@aws-amplify\core\lib\index.js:39:10)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
bug

All 10 comments

@powerful23 the pr u referenced already in 1.0.14-unstable.0 and it dont fix the issue

The correct way is to typeof window !== 'undefined'
And there is already helper in project for this
https://github.com/aws-amplify/amplify-js/blob/master/packages/core/src/JS.ts#L152

@aldarund sorry about that. I just approved another pr to fix that #1786

@powerful23 not quite follow, the pr #1786 u referenced now is for totally different piece of code. ANd that piece of code too have incorrect window checking

@aldarund try using a polyfill. set global["window"] = {};

related resources

  1. aws-appsync on node

use these polyfills

global.WebSocket = require('ws');
global.window = global.window || {
    setTimeout: setTimeout,
    clearTimeout: clearTimeout,
    WebSocket: global.WebSocket,
    ArrayBuffer: global.ArrayBuffer,
    addEventListener: function () { },
    navigator: { onLine: true }
};
global.localStorage = {
    store: {},
    getItem: function (key) {
        return this.store[key]
    },
    setItem: function (key, value) {
        this.store[key] = value
    },
    removeItem: function (key) {
        delete this.store[key]
    }
};

@aaayushsingh zzz. it should be fixed properly on lib level ( e.g. checking for typeof of window), not via hacks in user land code. There could be other libs used as well that check for window properly and setting it like that will make them think its browser and fail because of it.

@aldarund good point. I already know axios has issue with this approach. There was an issue earlier and I was told that aws-amplify primarily is meant for browser based environments. you'll probably need to use more polyfills.

This is a temporary fix you can use right now while AWS team looks into this.

Just for some context here on the overall goals for amplify. Amplify is a client side library and we want to stay focused on that (client-side not server-side, browser, mobile, RN etc). Adding actual support for node/server would take a lot more than just a global polyfill for example, a fetch polyfill, localstorage/cache, cookies etc. (and more). So adding a polyfill to just fill this window global object would not give support for node and we don't want to confuse people by implying our support for it further.

Right now im using it only for cognito auth, and it work fine if that check for window fixed properly

@aldarund sorry about that. I submitted another one for this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ddemoll picture ddemoll  路  3Comments

romainquellec picture romainquellec  路  3Comments

callmekatootie picture callmekatootie  路  3Comments

cosmosof picture cosmosof  路  3Comments

rygo6 picture rygo6  路  3Comments