1

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"
}
lras
  • 35
  • 5
  • What's in your `package.json` and what is the extension of the file you're running? – D. Pardal Jan 08 '21 at 16:45
  • Create a new file `test.js` in that file put one single line of code `var fs = require('fs');`. Run that file with node `node test.js`. If it errors, post that info along with your nodejs version. If it doesn't error, something is wrong with your other file. – Jared Smith Jan 08 '21 at 16:47
  • Is your main file an ECMAScript module file with a `.mjs` file extension? If so, there is no `require()` defined in that environment and you would use the `import` syntax to load external modules. – jfriend00 Jan 08 '21 at 16:53
  • 1
    I don't see any public modules called `strava_api_v3`, so can't test if there's a bug in that code (I assume it's a private module?). There are public modules `strava` and `strava-v3` that may work. – Zac Anger Jan 08 '21 at 16:53
  • 1
    @Liam - I don't think the OP is actually trying to run anything in a browser so your guidance so far does not seem to apply to running in plain nodejs. – jfriend00 Jan 08 '21 at 16:54
  • I added my package.json and another test, as requested – lras Jan 08 '21 at 17:02
  • @ZacAnger The issue is with `require`. – Dave Newton Jan 08 '21 at 17:06
  • What version of Node? – Dave Newton Jan 08 '21 at 17:07
  • @DaveNewton yeah, was wondering if maybe something in the strava module was possibly messing with `require` in some way. The edit clarifies things. – Zac Anger Jan 08 '21 at 17:08
  • My Node version is v14.15.3 – lras Jan 08 '21 at 17:09
  • If you just run `node`, and then in the Node repl type `require`, does it give you output that looks something like `[Function: require { resolve: [Function resolve` etc? – Zac Anger Jan 08 '21 at 17:11
  • @Zac Anger Yes, the output looks just like that – lras Jan 08 '21 at 17:13
  • Okay, that means your installation of Node isn't totally broken somehow, at least! Are you using the `mjs` extension anywhere? If so, you may need to shim require as explained in this answer: https://stackoverflow.com/a/64818509/5774952 – Zac Anger Jan 08 '21 at 17:17
  • @Zac Anger No, I don't see it anywhere – lras Jan 08 '21 at 17:38
  • 1
    Can you update the error message in your question with the full stack trace? – Zac Anger Jan 08 '21 at 17:42
  • This doesn't make a whole lot of sense then; do you have a Github/etc. of the project? `require` pretty much has to work--if it isn't, there's something very specific to your project and/or Node setup. – Dave Newton Jan 08 '21 at 17:42
  • Ahh, the stack trace in your edit clarifies things. The bottom three lines point to the ESM module loader being used, rather than CJS modules. Do you possibly have `node` aliased to node with flags? `type node` would tell you. Something is deciding that you want to use ESM rather than CJS somewhere. – Zac Anger Jan 08 '21 at 17:56
  • Nevermind, I see it, see my answer. We all missed that in the package.json – Zac Anger Jan 08 '21 at 17:57

2 Answers2

1

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.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
0

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. Then in your terminal run npm start and it should work.

Also, as Zac Anger says, remember to remove the "type": "module" line in your package.json. Otherwise you will see an error.