【问题标题】:npm script fails with sh: 1: <command>: not found in docker containernpm 脚本失败并显示 sh: 1: <command>: not found in docker container
【发布时间】:2020-06-19 15:12:44
【问题描述】:

问题

在 docker 容器中运行 npm run start:debug 命令时,出现以下错误:

# npm run start:debug

> api@0.0.0 start:debug /usr/src/api
> nest start -e "node --inspect-brk 0.0.0.0:9229" --watch -p tsconfig.json

sh: 1: nest: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! api@0.0.0 start:debug: `nest start -e "node --inspect-brk 0.0.0.0:9229" --watch -p tsconfig.json`
npm ERR! spawn ENOENT

运行npm ls --depth=0 表明我安装了@nestjs/cli

# npm ls --depth=0
api@0.0.0 /usr/src/api
+-- @nestjs/cli@6.14.2
+-- @nestjs/common@6.11.11
...

为什么找不到 nest cli 二进制文件?

我的设置

这就是我启动 shell 的方式:

docker-compose -f docker-compose-base.yml -f docker-compose-dev.yml run api /bin/sh

我的 docker-compose 文件:

# -base
version: '3'

services:
  api:
    build: .
    restart: on-failure
    volumes:
      - /usr/src/api/node_modules
    container_name: api


# -dev
version: '3'

networks:
  # Use lb_lbnet network created by the load balancer repo (lb)
  # We do this because we need the load balance to resolve container names defined here to forward traffic
  # This is only needed for dev
  default:
    external:
      name: lb_lbnet

services:
  db:
    image: postgres:11
    container_name: db
    restart: always
    env_file:
      - ./db.env # uses POSTGRES_DB and POSTGRES_PASSWORD to create a fresh db with a password when first run
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
      # only used to upload DB dump:
      # - ./backup:/tmp/backup

  api:
    restart: 'no'
    build:
      context: .
      args:
        NODE_ENV: development
    depends_on:
      - db
    ports:
      - 9229:9229
    volumes:
      - ./:/usr/src/api
      - ./node_modules:/usr/src/api/node_modules
      # enable to debug hgezim-express-shopify-auth
      - ../../hgezim-express-shopify-auth:/usr/hgezim-express-shopify-auth
    env_file:
      - .env
    command: /bin/bash -c 'echo "Starting" && npm install && npm run start:debug'

我的 Dockerfile:

FROM node:12

WORKDIR /usr/src/api

COPY package*.json ./

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

RUN npm install # && npm ls --depth=0 # commented this out since it returns non-zero exit code

COPY . .

VOLUME [ "/usr/src/api/node_modules" ]

RUN ["/usr/local/bin/npm", "run","lint"]
RUN ["/usr/local/bin/npm", "run","build"]

# not using an execution list here so we get shell variable substitution
CMD /bin/bash -c 'npm run start:$NODE_ENV'

【问题讨论】:

    标签: node.js docker npm docker-compose


    【解决方案1】:

    需要全局安装 Nest CLI 才能使命令行正常工作。看起来您已通过 package.json 在本地安装它,因此未将嵌套添加到 PATH。将RUN npm install -g @nestjs/cli 添加到您的Dockerfile,或更改start:debug 脚本以使用本地版本(类似于node_modules/&lt;nestcli module&gt;/.bin/nest)。

    【讨论】:

      猜你喜欢
      • 2011-02-12
      • 1970-01-01
      • 2019-07-01
      • 2015-12-02
      • 2016-01-15
      • 2022-01-26
      • 2020-01-28
      • 2020-09-26
      • 2015-05-28
      相关资源
      最近更新 更多