简体   繁体   English

在 node.js 中运行时未定义要求

[英]Require is not defined when running in node.js

I'm trying to access the Strava API, but terminal is showing我正在尝试访问 Strava API,但终端显示

ReferenceError: require is not defined
at file:///Users/Me/directory/code.js:1:10
at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
at async Loader.import (internal/modules/esm/loader.js:166:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)

when I try to run the code from their website in terminal.当我尝试在终端中从他们的网站运行代码时。

I've researched it, and it says that it's because I'm running it on a browser, but I don't think I am.我研究过它,它说这是因为我在浏览器上运行它,但我不认为我是。 I commented out all of the code so the only line was我注释掉了所有的代码,所以唯一的一行是

var StravaApiV3 = require('strava_api_v3');

and the error still keeps showing up.并且错误仍然不断出现。 I'm just running it with "cd directory" and then node code.js (the names of the directory and code have been changed for clarity), so I have no clue why it isn't working.我只是用“cd 目录”运行它,然后是 node code.js(为了清楚起见,已经更改了目录和代码的名称),所以我不知道它为什么不起作用。 Is the one line of code somehow going to a browser?一行代码是否以某种方式进入浏览器? I'm running it with MacOS Big Sur, if that changes anything.我正在使用 MacOS Big Sur 运行它,如果这有什么改变的话。

Here's the rest of the code that Strava has on their website, if you need it:这是 Strava 在其网站上的代码的 rest,如果您需要它:

    var StravaApiV3 = require('strava_api_v3');
var defaultClient = StravaApiV3.ApiClient.instance;

// Configure OAuth2 access token for authorization: strava_oauth
var strava_oauth = defaultClient.authentications['strava_oauth'];
strava_oauth.accessToken = "YOUR ACCESS TOKEN"

var api = new StravaApiV3.ActivitiesApi()

var id = 789; // {Long} The identifier of the activity.

var opts = { 
  'includeAllEfforts': true // {Boolean} To include all segments efforts.
};

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
api.getActivityById(id, opts, callback);

TL;DR this code is giving me the "require is not defined" error even though I'm running it with node.js TL;DR 即使我使用 node.js 运行此代码,此代码仍给我“未定义要求”错误

EDITS:编辑:

-When I run a dummy require (it's just var fs = require('fs');) in the same directory, it still gives the same error - 当我在同一个目录中运行一个虚拟要求(它只是 var fs = require('fs');)时,它仍然给出相同的错误

-My package.json for the directory is - 我的 package.json 的目录是

    {
  "name": "stravatest",
  "version": "1.0.0",
  "description": "description",
  "main": "stravatest.js",
  "type":"module",
  "scripts": {
    "test": "test"
  },
  "repository": {
    "type": "git",
    "url": "url"
  },
  "keywords": [
    "keyword"
  ],
  "author": "author",
  "license": "ISC"
}

Remove "type": "module" from your package.json.从 package.json 中删除"type": "module" This tells Node you're using ESM rather than CJS, but that's not what you want to be doing (unless you do, in which case you should use the .mjs extension and use import instead of require .这告诉 Node 你使用的是 ESM 而不是 CJS,但这不是你想要做的(除非你这样做,在这种情况下你应该使用.mjs扩展名并使用import而不是require

But how exactly are you running the project?但是你究竟是如何运行这个项目的? You should add a "start" script in your package.json with the following command: node stravatest.js .您应该使用以下命令在 package.json 中添加“启动”脚本: node stravatest.js Then in your terminal run npm start and it should work.然后在您的终端中运行npm start它应该可以工作。

Also, as Zac Anger says, remember to remove the "type": "module" line in your package.json.此外,正如Zac Anger所说,请记住删除 package.json 中的"type": "module"行。 Otherwise you will see an error.否则你会看到一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM