ReferenceError:未定义文档(JavaScript)

时间:2020-10-21 16:33:49

标签: javascript docker

我有一个奇怪的问题,我可以在本地运行页面,但不能在Docker上运行页面?

这是来自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如下:

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);

Docker文件似乎是正确的,因为我之前设法部署过REST API,只是html似乎会引起问题。

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

我将不胜感激:)

0 个答案:

没有答案