Javascript 需要未定义 nodejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38343751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Require is not defined nodejs
提问by johnnE
Trying to use this smartsheet api: http://smartsheet-platform.github.io/api-docs/?javascript#node.js-sample-code
尝试使用这个 smartsheet api:http://smartsheet-platform.github.io/api-docs/?javascript#node.js-sample-code
and its telling me to do this for nodejs:
并告诉我为 nodejs 执行此操作:
var client = require('smartsheet');
var smartsheet = client.createClient({accessToken:'ACCESSTOKEN'});
So i do this in my main.js file but I get the error: Uncaught ReferenceError: require is not defined
因此,我在 main.js 文件中执行此操作,但出现错误:未捕获的 ReferenceError:需要未定义
I think its because im new to nodejs/npm but I cannot find it anywhere where to actually put this require function. I think i need to mess with my node.js file but im note entirely sure. Any link to documentation or suggestions are greatly appreciated!
我认为这是因为我是 nodejs/npm 的新手,但我无法在任何地方找到它来实际放置这个 require 函数。我想我需要弄乱我的 node.js 文件,但我完全确定。非常感谢任何指向文档或建议的链接!
采纳答案by Shubham Mittal
This is because you are using node-specific calls in the browser! NodeJS is a server-side technology and not a browser technology. Thus, node-specific calls will only work in the server.
这是因为您在浏览器中使用了特定于节点的调用!NodeJS 是一种服务器端技术,而不是浏览器技术。因此,特定于节点的调用只能在服务器中工作。
The smartsheetapi you're trying to use needs to be called from the server-side code and not client side code.
您尝试使用的smartsheetapi 需要从服务器端代码而不是客户端代码调用。
For your case, you can set up ExpressJSand create an dummy api which internally calls the smartsheet api.
对于您的情况,您可以设置ExpressJS并创建一个内部调用 smartsheet api 的虚拟 api。
If you indeed want to use such calls on the client side, you can use CommonJSclient side-implementations
如果您确实想在客户端使用此类调用,则可以使用CommonJS客户端实现
回答by gyan deep
Try Removing "type": "module", from package.json
尝试从 package.json 中删除 "type": "module"
回答by Hannan
require()
is a function provided in NodeJS
and it wont work on client/browser
because there is nothing like require()
on client side
require()
是一个提供的功能NodeJS
,它不会工作,client/browser
因为require()
在客户端没有任何东西
回答by Jay
you can't use require
directly in browser. you need RequireJS, a JavaScript module loader.
不能require
直接在浏览器中使用。你需要RequireJS,一个 JavaScript 模块加载器。
This is introductory note from RequireJS.
这是RequireJS 的介绍性说明。
RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.
RequireJS 是一个 JavaScript 文件和模块加载器。它针对浏览器内使用进行了优化,但也可以在其他 JavaScript 环境中使用,如 Rhino 和 Node.js。使用像 RequireJS 这样的模块化脚本加载器将提高代码的速度和质量。
回答by Dimitry Ivanov
require() does not exist in the browser/client-side JavaScript
More info on Client on node: Uncaught ReferenceError: require is not defined