Cannot find module express: docker

huangapple go评论58阅读模式
英文:

Cannot find module express: docker

问题

FROM node:18.16.0

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

我有这个Dockerfile,运行docker-compose up时出现错误:
Error: 无法找到模块'express'

我尝试更新了Dockerfile但没有成功


<details>
<summary>英文:</summary>

```docker
FROM node:18.16.0

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [&quot;npm&quot;, &quot;start&quot;]

I have this Dockerfile, when I run docker-compose up I get this error :
Error: Cannot find module 'express'

I tried updating the docker file but it did not work

答案1

得分: -1

"dependencies": {
"dependency1": "^v-xxx",
"dependency2": "^v-xxx",
"express": "^4.17.1" (version-4.17 is just an example)
}

不建议的方式:(在 Dockerfile 中更改这些)

RUN npm install
RUN npm install express

话虽如此,您需要以一种方式更改您的 Dockerfile,以便在服务器启动后不会退出。希望这有所帮助。

英文:

As pointed out by Sachin, we need to add express dependency in package.json file.

Better solution:

&quot;dependencies&quot;: {
    &quot;dependency1&quot;: &quot;^v-xxx&quot;,
    &quot;dependency2&quot;: &quot;^v-xxx&quot;,
    &quot;express&quot;: &quot;^4.17.1&quot;  (version-4.17 is just an example)
  }

Not recommended way: (Alter these in Dockerfile)

RUN npm install
RUN npm install express

Being said that you need to alter your Dockerfile in a way that it will not exit once the server is started. Hope this helps.

huangapple
  • 本文由 发表于 2023年5月14日 09:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定