【发布时间】:2016-12-30 02:20:43
【问题描述】:
我正在尝试在新的 React Web 应用程序上使用 Google API Node.js client。 这是我的 package.json
{
"name": "onboarding",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-scripts": "0.8.4"
},
"dependencies": {
"googleapis": "^15.1.0",
"react": "^15.4.1",
"react-dom": "^15.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
当我尝试访问 App.js 中的 API 时:
var google = require('googleapis');
它不会编译并给我:
Failed to compile.
Error in ./~/google-auth-library/lib/auth/googleauth.js
Module not found: 'child_process' in /home/jack/onboarding/node_modules/google-auth-library/lib/auth
@ ./~/google-auth-library/lib/auth/googleauth.js 21:11-35
Error in ./~/googleapis/lib/generator.js
Module not found: 'swig' in /home/jack/onboarding/node_modules/googleapis/lib
@ ./~/googleapis/lib/generator.js 21:11-26
Error in ./~/googleapis/lib/generator.js
Module not found: 'js-beautify' in /home/jack/onboarding/node_modules/googleapis/lib
@ ./~/googleapis/lib/generator.js 22:15-37
我可以通过手动将js-beautify 和swig 添加到依赖项来消除错误,但这不适用于child_process。无论哪种方式,我都觉得我在这里做错了什么。 var google = require('googleapis'); 不应该在 react App.js 代码中吗?任何建议将不胜感激。
【问题讨论】:
-
你试过
rm -rf node_modules并做一个新鲜的npm install吗? -
是的,我有。我想知道我是否不应该将
var google = require('googleapis');放入 react App.js 文件中 -
你是对的。
googleapis是一个适用于所有 Google API 的节点库。不能在前端使用。 -
他们提供可以从前端使用的库吗?
标签: javascript node.js reactjs google-api-nodejs-client