5

I am trying to get a simple MERN app working on an Ubuntu wsl2 instance. I am following this guide. This is the code that I have in server.js (it is slightly different to the code in the guide as body-parser is deprecated. Using the advice from this post I've changed those methods).

const express = require("express");
const mongoose = require("mongoose");

// Setup express app
const app = express();

app.use(
    express.urlencoded({
        extended: false
    })
);

app.use(express.json());

// Configure Mongo
const db = "mongodb://localhost/313-demo-mern-db";

// Connect to Mongo with Mongoose
mongoose
    .connect(
        db,
        { useNewUrlParser: true }
    )
    .then(() => console.log("Mongo connected"))
    .catch(err => console.log(err));

// Specify the Port where the backend server can be accessed and start listening on that port
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server up and running on port ${port}.`));

I have a MongoDB database running where it is currently "Waiting for connections on port 27017". I run the command node server.js and get the error.

/home/NAME/learnreact/MERN-demo/node_modules/whatwg-url/dist/encoding.js:2
const utf8Encoder = new TextEncoder();
                    ^

ReferenceError: TextEncoder is not defined
    at Object.<anonymous> (/home/NAME/learnreact/MERN-demo/node_modules/whatwg-url/dist/encoding.js:2:21)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/NAME/learnreact/MERN-demo/node_modules/whatwg-url/dist/url-state-machine.js:5:34)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
redpanda2236
  • 174
  • 7

1 Answers1

4

I was having the same error today. Updating the node to latest version fixed the issue for me.

You may refer https://askubuntu.com/a/480642/1267099 for updating node.

edit: update to the latest version (not the current stable)