要求未定义nodejs

Require is not defined nodejs

正在尝试使用此智能表 api:http://smartsheet-platform.github.io/api-docs/?javascript#node.js-sample-code

它告诉我为 nodejs 做这个:

var client = require('smartsheet');
var smartsheet = client.createClient({accessToken:'ACCESSTOKEN'});

所以我在我的 main.js 文件中这样做,但我收到错误:Uncaught ReferenceError: require is not defined

我认为这是因为我是 nodejs/npm 的新手,但我无法在实际放置此 require 函数的任何地方找到它。我想我需要弄乱我的 node.js 文件,但我完全确定。非常感谢任何 link 文档或建议!

require() does not exist in the browser/client-side JavaScript

有关 Client on Node.js: Uncaught ReferenceError: require is not defined

的更多信息

这是因为您在浏览器中使用了特定于节点的调用! NodeJS 是一种服务器端技术,而不是浏览器技术。因此,特定于节点的调用将仅在服务器中有效。

您尝试使用的 smartsheet api 需要从服务器端代码而不是客户端代码调用。

对于您的情况,您可以设置 ExpressJS 并创建一个虚拟 api,它在内部调用智能表 api。

如果您确实想在客户端使用此类调用,您可以使用 CommonJS 客户端实现

您不能直接在浏览器中使用 require。你需要 RequireJS,一个 JavaScript 模块加载器。

这是 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.

尝试从 package.json

中删除 "type": "module"

将所有 require 语句替换为 import 语句。示例:

//BEFORE:
var client = require('smartsheet');
//AFTER:
import client from 'smartsheet';

对我有用。

从技术上讲,它是内置在浏览器中的,最好的办法是在项目中禁用 es-lint,所有这些错误都不会发生。 或者尝试评论整个 es-lint 文件。

对我有用!