'"ReferenceError: process is not defined" when connecting and saving to Mongoose in TypeScript from React component

When commenting out the sendScore() call from App.tsx, the error disappears. Mongoose is listed as dependency ("mongoose": "^6.3.1",) in my package.json file. process is a Node.js global variable.

Console:

Uncaught ReferenceError: process is not defined
    at Object.1490 (browser.umd.js:2:1)
    at r (browser.umd.js:2:1)
    ...
    ./src/lib/sendScore.ts  @   sendScore.ts:1
    ...
    ./src/App.tsx   @   index.js:1

App.tsx:

// ...
import { sendScore } from './lib/sendScore'

function App() {
  // ...
  const onEnter = () => {
    // ...
    sendScore(solutionIndex, solution, guesses, false, isHardMode)
  }
}

export default App

sendScore.ts:

const mongoose = require('mongoose')
const password = '<PASSWORD>'
const url = `mongodb+srv://username:${password}@cluster0.xrcmw.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`

mongoose.connect(url)
const scoreSchema = new mongoose.Schema({
    solutionIndex: Number,
    solution: String,
    guesses: [String],
    lost: Boolean,
    isHardMode: Boolean,
})

const Score = mongoose.model('Score', scoreSchema)

export const sendScore = (solutionIndex: number, solution: string, guesses: string[], lost: boolean, isHardMode: boolean) => {
    const score = new Score({
        solutionIndex: solutionIndex,
        solution: solution,
        guesses: guesses,
        lost: lost,
        isHardMode: isHardMode,
    })
    
    score.save().then(() => {
        console.log('Score sent')
    })
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source