2

我只是想用 NextJS 和 aws-amplify 运行一个基本的 hello world,但似乎在我 npm install 这两个库的那一刻

aws-amplify & aws-amplify-react

我得到“缺少反应模块”并且没有定义窗口。

import React from 'react'
import Amplify from 'aws-amplify';

Amplify.configure({
    Auth: {
        // REQUIRED - Amazon Cognito Identity Pool ID
        identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
        // REQUIRED - Amazon Cognito Region
        region: 'XX-XXXX-X',
        // OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: 'XX-XXXX-X_abcd1234',
        // OPTIONAL - Amazon Cognito Web Client ID
        userPoolWebClientId: 'XX-XXXX-X_abcd1234',
    }
});

export default class extends React.Component {
    static async getInitialProps({ req }) {
        const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
        return { userAgent }
    }

    render() {
        return (
            <div>
              Hello World
              <style jsx>{`

            `}</style>
            </div>
        )
    }
}
4

2 回答 2

2

您需要制作某种 polyfill 以避免该window is not defined错误。另外,也许您需要检查您的node_modules文件夹以查看是否react正确安装。

polyfill 示例:```

(<any>global).window = (<any>global).window || {};
(<any>global).localStorage = (<any>global).localStorage || {
    store: {},
    getItem(key){
        if (this.store[key]) {
            return this.store[key];
        }
        return null;
    },
    setItem(key, value){
        this.store[key] = value;
    },
    removeItem(key){ delete this.store[key]; }
};

```

于 2018-04-04T23:25:27.013 回答
0

尝试这个。安装tslib然后测试

npm 安装 tslib

于 2020-04-13T10:47:00.917 回答