简体   繁体   English

npm 错误! 缺少脚本:服务

[英]npm ERR! missing script: serve

Getting error while starting the docker container.启动 docker 容器时出错。 I am using nodemon to listen to the file changes.我正在使用 nodemon 来监听文件更改。

DockerFile文件

FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm","run","serve"]

Package.json包.json

{
    "dependencies": {
        "express": "*",
        "nodemon": "*"
    },
    "scripts": {
        "serve": "nodemon index.js",
        "start": "node index.js"
    }
}

build command构建命令

docker build -f Dockerfile.dev -t test/nodeapp1 .

cmdLine docker cmd -> cmdLine docker cmd ->

docker run -p 3000:8080 -v /app/node_modules -v pwd:/app test/nodeapp1.

Iam new to docker, and not able to figure out the cause.我是 docker 新手,无法找出原因。

Make this changes in your dockerfile在您的 dockerfile 中进行此更改

FROM node:alpine

ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV HOME=/home/node/app
ENV PATH="/home/node/.npm-global/bin:${PATH}"

USER node

RUN npm install -g nodemon


RUN mkdir -p ${HOME}
WORKDIR ${HOME}

ADD package.json ${HOME}

RUN cd ${HOME} && npm install

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

Build the docker container构建 docker 容器

docker build -f Dockerfile -t prac/nodeApp .

Run the docker container运行docker容器

docker run -p 3000:8080 -v /app/node_modules -v pwd:/app prac/nodeApp

Changing the WORKDIR to a new value worked.将 WORKDIR 更改为新值有效。

FROM node:alpine

WORKDIR '/dir'

COPY package.json .

RUN npm install

COPY . .

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

Your docker run -v options are wrong.你的docker run -v选项是错误的。 You probably actually meant to write你可能真的想写

docker run ... -v $PWD:/app ...
docker run ... -v $(pwd):/app ...

to use the current directory (from the PWD environment variable or from the pwd command, respectively) as a bind mount .使用当前目录(分别来自PWD环境变量或pwd命令)作为绑定安装

I tend to not recommend this pattern, especially for Node applications where the host dependencies are minimal and you're not interacting much with other containers.我倾向于不推荐这种模式,特别是对于主机依赖关系很小并且您与其他容器交互不多的 Node 应用程序。 It's probably easier to just install Node locally (if you don't already have it) and do live development against that;仅在本地安装 Node(如果您还没有它)并针对它进行实时开发可能更容易; when you want to use Docker to deploy your application, use the version you've COPY ed into the image, and don't separately use a -v option to inject your code over it.当您想使用 Docker 部署应用程序时,请使用您COPY到映像中的版本,并且不要单独使用-v选项在其上注入您的代码。

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

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