0

I've got a weird problem, where I can run my page locally, but not on Docker?

Here is the error snippet from docker:

/app/script.js:1

(function (exports, require, module, __filename, __dirname) { const daysEl = window.document.getElementById("days");

^


ReferenceError: window is not defined

at Object.<anonymous> (/app/script.js:1:78)

at Module._compile (module.js:571:32)

at Object.Module._extensions..js (module.js:580:10)

at Module.load (module.js:488:32)

at tryModuleLoad (module.js:447:12)

at Function.Module._load (module.js:439:3)

at Module.runMain (module.js:605:10)

at run (bootstrap_node.js:427:7)

at startup (bootstrap_node.js:151:9)

at bootstrap_node.js:542:3

script.js goes as follow:

const daysEl = window.document.getElementById("days");
const hoursEl = window.document.getElementById("hours");
const minsEl = window.document.getElementById("mins");
const secondsEl = window.document.getElementById("seconds");

const newYears = "1 Jan 2021";

function countdown() {
    const newYearsDate = new Date(newYears);
    const currentDate = new Date();

    const totalSeconds = (newYearsDate - currentDate) / 1000;

    const days = Math.floor(totalSeconds / 3600 / 24);
    const hours = Math.floor(totalSeconds / 3600) % 24;
    const mins = Math.floor(totalSeconds / 60) % 60;
    const seconds = Math.floor(totalSeconds) % 60;

    daysEl.innerHTML = days;
    hoursEl.innerHTML = formatTime(hours);
    minsEl.innerHTML = formatTime(mins);
    secondsEl.innerHTML = formatTime(seconds);
}

function formatTime(time) {
    return time < 10 ? `0${time}` : time;
}

// initial call
countdown();

setInterval(countdown, 1000);

The Docker file seems to be correct, as I managed to deploy rest api before, it's just html thatm seems to cause problems.

FROM node:7
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node script.js
EXPOSE 8082

I will be grateful for any help :)

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
Napior
  • 1
  • 1
  • Also tried with document.getElementById("days") but with no luck – Napior Oct 21 '20 at 16:35
  • 1
    The code is running in node on docker? `document` exists in browsers. Are you running the code in a headless browser? Or using JSDom? – evolutionxbox Oct 21 '20 at 16:35
  • 3
    Does this answer your question? [Node.js document is not defined](https://stackoverflow.com/questions/32126003/node-js-document-is-not-defined) – evolutionxbox Oct 21 '20 at 16:36
  • JavaScript and the web browser are not the same thing. To control the browser from a server, try https://github.com/puppeteer/puppeteer https://github.com/SeleniumHQ/selenium – noɥʇʎԀʎzɐɹƆ Oct 21 '20 at 16:39

0 Answers0