skip to Main Content

I’m encountering the following error while trying to launch my docker container:

Error:

server_1  | /usr/src/app/node_modules/whatwg-url/lib/encoding.js:2
server_1  | const utf8Encoder = new TextEncoder();
server_1  |                     ^
server_1  | 
server_1  | ReferenceError: TextEncoder is not defined

Dockerfile:

FROM --platform=linux/amd64 node:current-alpine

WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "run", "prod"]

docker-compose.yml:

version: '3'

services:

  server:
    build:
      context: ./server
    ports:
      - "3008:3002"
    environment:
      API_PORT: "3002"
      DB_HOST: "192.168.1.211"
      DB_PORT: "27018"
      DB_NAME: "tutorialitems"
    restart: always

index.js file:

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();

app.use(cors());
app.use(bodyParser({ limit: '50mb' }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

//DB
const db = require('./models');
const dbConfig = require('./config/db.config');

try {
    db.mongoose.connect(`mongodb://${dbConfig.HOST}:${dbConfig.PORT}/${dbConfig.DB}`, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    })
    console.log('DB Connected!');
} catch (error) {
    console.log('Cannot connect to DB...', error);
    process.exit();
}

//Routes
require('./routes/crud.routes')(app);

app.get('/', (req, res) => {
    res.json("Server is Running")
});

app.listen(process.env.API_PORT, () => {
    console.log(`Server is listening on port ${process.env.API_PORT}`)
});

What I tried (but unsuccessfully):
I updated the following file: node_modules > whatwg-url > lib > encoding.js from
Initial:

"use strict";
const utf8Encoder = new TextEncoder();
const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });

to Updated:

const util = require("util");
const utf8Decoder = new util.TextEncoder("utf-8", { ignoreBOM: true });

package.json:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon index.js",
    "prod": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.2",
    "mongoose": "^6.1.5",
    "nodemon": "^2.0.15"
  }
}

But when I run my docker-compose, I still have the same error.

Anyone has an idea?

Thks

2

Answers


  1. Chosen as BEST ANSWER

    Mongoose v6.1.5 is working with Node 17.3.0.

    Package.json

    {
      "name": "server",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "dev": "nodemon index.js",
        "prod": "node index.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "body-parser": "^1.19.0",
        "cors": "^2.8.5",
        "dotenv": "^10.0.0",
        "express": "^4.17.2",
        "mongoose": "^6.1.5",
        "nodemon": "^2.0.15"
      }
    }
    

    After updating my package.json with the Mongoose 6.1.5 version, and my Dockerfile as following:

    FROM --platform=linux/amd64 node:17.3.0
    
    WORKDIR /usr/src/app
    COPY package*.json ./
    
    RUN npm install
    
    COPY . .
    
    CMD ["npm", "run", "prod"]
    

    I run the following docker-compose commands:

    docker-compose pull
    docker-compose build
    docker-compose up
    

    And now it's working. It seems that docker-compose up was still using the previously build version... thus, it was not working...


  2. package.json:
    "mongoose": "^6.2.7"

    DockerFile:
    FROM node:17-slim

    I was getting the same error, when I set the versions as above (node:16-slim to 17-slim), my problem went away. Thanks..

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search