'cannot call googleapis library inside html
I've been trying to call a simple function (uploadFile(test.txt)
) inside an html but I can not call neither my app.js or googleapis library
node.js gives this eror.
Uncaught ReferenceError: uploadFile is not defined
Uncaught Error: Script error for "googleapis"
Uncaught Error: Script error for "path"
Uncaught Error: Script error for "fs"
Uncaught Error: Script error for "googleapis/build/src/apis/deploymentmanager"
path fs and other libraries are called inside app.js using require.js
like this;
requirejs.config({
baseUrl: 'js/lib',
});
requirejs(['googleapis', 'path', 'fs', 'googleapis/build/src/apis/deploymentmanager'],
function (google, path, fs, deploymentmanager) {
..."rest of the code"...}
my index.html file;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello</title>
<script data-main="js/app.js" src="js/require.js"></script>
</head>
<body>
<div id="root"></div>
<script>
uploadFile("test.txt")
</script>
</body>
</html>
Solution 1:[1]
The arguments you pass to requirejs
need to be URLs to AMD modules.
You are passing in the names (not URLs) of CommonJS modules, most of which are Node.js built-ins.
The require
function from RequireJS and the require
function built into Node.js solve similar problems but are not remotely compatible.
You cannot use RequireJS to load CommonJS modules. You cannot use modules which depend on Node.js features (like fs
) in a web browser.
If you want to use a Google API in a browser, then use the library that Google provide for accessing it from a browser. Note that Google does not intend some APIs to be used from a browser at all and you will need to use server-side code to access them.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Quentin |