Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

javascript - Require is not defined when running in node.js

I'm trying to access the Strava API, but terminal is showing

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. Is the one line of code somehow going to a browser? I'm running it with MacOS Big Sur, if that changes anything.

Here's the rest of the code that Strava has on their website, if you need it:

    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

EDITS:

-When I run a dummy require (it's just var fs = require('fs');) in the same directory, it still gives the same error

-My package.json for the directory is

    {
  "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"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove "type": "module" from your package.json. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...