简体   繁体   English

无法通过Docker容器运行node.js应用

[英]Unable to run node.js app through Docker container

I have a node.js application which I am trying to run in docker. 我有一个要在docker中运行的node.js应用程序。 Here is the package.json file snippet. 这是package.json文件片段。

"main": "lib/server.js",
  "scripts": {
    "clean": "cross-env rm -rf dist",
    "build": "cross-env babel lib -d dist",
    "start": "npm run clean && npm run build && npm run serve",
    "serve": "node dist/index.js",
    "dev": "cross-env NODE_ENV=development nodemon lib/server.js --exec babel-node",
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "eslint --ext .js lib/ scripts/ --ignore-pattern node_modules/"
  }

And here is the Dockerfile to build the image 这是构建映像的Dockerfile

#---- Base Node------
FROM node:10.15.1-alpine

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package*.json ./

COPY lib ./lib

RUN npm install --only=production && npm run build

EXPOSE 4201

CMD ["npm", "run", "serve"]

I get the following error when I try to run the image. 尝试运行图像时出现以下错误。 Here is the screenshot of the error. 这是错误的屏幕截图。

在此处输入图片说明

The node and npm version are the same on my machine and the base image that I am using to build my image. 我的机器和用于构建映像的基本映像在节点和npm版本上都是相同的。 When I run the same command locally in my machine it works Here is the exact command that I am running on my machine and it works. 当我在计算机上本地运行同一命令时,它可以正常工作这是我在计算机上运行且可以运行的确切命令。

npm install --only=production npm install --only =生产

npm run build npm运行构建

npm run serve npm运行发球

Am I doing something wrong here ? 我在这里做错什么了吗? Any help is appreciated. 任何帮助表示赞赏。

You are using ES6 imports 您正在使用ES6导入

import x from package 从包中导入x

In order to do that, you should have Babel package installed, otherwise it won't know how to import that file 为此,您应该安装Babel软件包,否则它将不知道如何导入该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM