I’m encountering a ‘ReferenceError: Request is not defined’ error every time I run npm start in my Node.js environment, and I suspect it’s stemming from the environmental file "node_modules/@expo/server/src/environment.ts".
Here’s a snippet of the environment file:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installGlobals = exports.ExpoResponse = exports.ExpoRequest = void 0;
/* eslint-disable no-var */
const node_1 = require("@remix-run/node");
/** @deprecated */
exports.ExpoRequest = Request;
/** @deprecated */
exports.ExpoResponse = Request;
function installGlobals() {
(0, node_1.installGlobals)();
global.ExpoRequest = Request;
global.ExpoResponse = Response;
}
exports.installGlobals = installGlobals;
However, I’m unsure why this error is occurring. Whenever I start Metro Bundler, it throws the error:
ReferenceError: Request is not defined
at Object. (/Users/Theo/nunj/node_modules/@expo/server/src/environment.ts:23:28)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/Users/Theo/nunj/node_modules/@expo/server/install.js:1:28)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
I would appreciate any insights or suggestions for resolving this issue. Thank you!
2
Answers
You may need to upgrade to v18 based on this change log published by Expo.
https://expo.dev/changelog/2023/11-03-node-default
It highlights that node v16 reached it’s end-of-life and encourages you to upgrade to v18.
I personally like to use a package called
n
that let’s me change node versions on the fly:https://www.npmjs.com/package/n
I was using Node
v16.x
so I had to switch to18.x
and currently my18.x
isv18.19.1
.npx use 18
As per expo docs here is the default node version https://expo.dev/changelog/2023/11-03-node-default