Build fails with ReferenceError: window is not defined

Build fails with ReferenceError: window is not defined

当我运行gridsome build我得到

ReferenceError: window is not defined
at Object. (src/gapi_client.js:1:9)
at webpack_require (webpack/bootstrap:25:0)
at Module. (assets/js/app.f322a6ac.js:68956:19)
at webpack_require (webpack/bootstrap:25:0)
at Object. (assets/js/app.f322a6ac.js:56344:18)
at webpack_require (webpack/bootstrap:25:0)
at assets/js/app.f322a6ac.js:118:18
at Object. (assets/js/app.f322a6ac.js:121:10)
at o (/home/s/somegrid/node_modules/vue-server-renderer/build.prod.js:1:77607)
at /home/s/somegrid/node_modules/vue-server-renderer/build.prod.js:1:78200

我的 main.js 在最上面有 import './gapi_client'

我的环境:

System:
OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver)
CPU: (2) x64 AMD A6-4400M APU with Radeon(tm) HD Graphics
Binaries:
Node: 11.6.0 - /usr/local/bin/node
Yarn: 1.15.2 - ~/.yarn/bin/yarn
npm: 6.13.6 - /usr/local/bin/npm
Browsers:
Chrome: 81.0.4044.113
Firefox: 75.0
npmPackages:
gridsome: ^0.7.14 => 0.7.14
npmGlobalPackages:
@gridsome/cli: 0.3.1

一些问题:

  1. 为什么 gapi_client.js 引用 window?

  2. 只有当process.isClient为真时才应该导入gapi_client.js吗?

我把main.js中的代码改成如下:

if (process.isClient) {
window.gapi_client = require('./gapi_client')
}

现在我的构建似乎通过了那一步,但失败了

Error: No native build was found for runtime=node abi=67 platform=linuxglibc arch=x64
at Function.module.exports.load.path (node_modules/node-gyp-build/index.js:56:0)
at load (node_modules/node-gyp-build/index.js:18:0)
at Object. (node_modules/leveldown/binding.js:1:41)
at Object. (assets/js/app.ce34d4e7.js:28227:30)
at webpack_require (webpack/bootstrap:25:0)
at Object. (node_modules/leveldown/leveldown.js:3:16)
at webpack_require (webpack/bootstrap:25:0)
at Object. (node_modules/level/level.js:1:43)
at webpack_require (webpack/bootstrap:25:0)
at Object. (assets/js/app.ce34d4e7.js:9411:13)

尝试 process.browser 而不是 process.isClient

我将代码更改为:

if (process.isClient) {
    window.gapi_client = require('./gapi_client').default
}

而且有效!