Cannot find module express: docker

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

Cannot find module express: docker

问题

  1. FROM node:18.16.0
  2. WORKDIR /usr/src/app
  3. COPY package*.json ./
  4. RUN npm install
  5. COPY . .
  6. EXPOSE 3000
  7. CMD ["npm", "start"]

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. ```docker
  4. FROM node:18.16.0
  5. WORKDIR /usr/src/app
  6. COPY package*.json ./
  7. RUN npm install
  8. COPY . .
  9. EXPOSE 3000
  10. 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:

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

Not recommended way: (Alter these in Dockerfile)

  1. RUN npm install
  2. 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:

确定