npm script fails with sh: 1: <command>: not found in docker container - node.js

Question
When running npm run start:debug command inside a docker container, I get this error:
# 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
Running npm ls --depth=0 shows that I have #nestjs/cli installed:
# npm ls --depth=0
api#0.0.0 /usr/src/api
+-- #nestjs/cli#6.14.2
+-- #nestjs/common#6.11.11
...
Why isn't the nest cli binary being found?
My Setup
This is how I launch the shell:
docker-compose -f docker-compose-base.yml -f docker-compose-dev.yml run api /bin/sh
My docker-compose files:
# -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'
My 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'

Nest CLI needs to be installed globally for the command line to work. Looks like you have it installed locally via package.json so nest was not added to PATH. Either add RUN npm install -g #nestjs/cli to your Dockerfile, or change start:debug script to use the local version (something like node_modules/<nestcli module>/.bin/nest).

Related

laravel 8 on Docker Vite manifest not found at: ...\public\/build/manifest.json with

i have problem to do npm run dev on docker on laravel project, i am using vue js authentication and i need to do npm run dev on docker and i need help
here is the content of my Dockerfile
FROM node:14-alpine as node
WORKDIR /var/www
COPY . .
RUN npm install --global cross-env
RUN npm install
RUN npm run build`
EXPOSE 3000
VOLUME /var/www/node_modules
here is the content of my docker-compose.yml
node:
build:
context: .
target: node
volumes:
- .:/usr/src
- ./node_modules:/usr/src/node_modules
command: npm run dev
tty: true
how should i go about solving this problem npm install && npm run dev ?
thank you in advance for your help

Dockerfile can not see package.json file

I've got this structure of the project:
- project
-- apps
--- microservice-one
---- Dockerfile
-- package.json
-- docker-compose.yml
Here is my Dockerfile from the microservice-one:
FROM node:12.13-alpine As development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
CMD ["node", "dist/main"]
and docker-compose.yml
services:
microservice-one:
container_name: microservice-one
build:
context: ./apps/microservice-one
dockerfile: Dockerfile
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
expose:
- '3000'
command: npm run start
why after run docker-compose build, my console throw me this error:
#12 0.725 npm ERR! code ENOENT
#12 0.726 npm ERR! syscall open
#12 0.728 npm ERR! path /usr/src/app/package.json
#12 0.729 npm ERR! errno -2
#12 0.734 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
I think that the problem is in my Dockerfile and COPY package*.json ./, is a chance that my Dockerfile can not see package.json?
Thanks for any help!
When your context is ./apps/microservice-one, the Dockerfile can only copy files from that directory and directories below it.
Your Dockerfile is written as if it assumes that the context is the current directory, so if you change the context and the dockerfile values in the docker-compose file, it should work.
services:
microservice-one:
container_name: microservice-one
build:
context: .
dockerfile: ./apps/microservice-one/Dockerfile
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
expose:
- '3000'
command: npm run start
From your filesystem structure the directory hosting Dockerfile is the build context -> microservice-one.
When you perform a docker build ..., the build context and all it's content gets wrapped up and sent over to the docker daemon, which then tries to build your container. At that time there is only access to the build context, nothing outside. ( https://docs.docker.com/engine/reference/commandline/build/#build-with-path )
So unless you move/copy your file over it will remain invisible.

Docker-compose up : no such file or directory, open '/usr/src/app/package.json'

I'm using docker and docker-compose to run my express nodejs api.
Here is my docker file:
FROM node:10-alpine
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN chmod 755 /usr/src/app
CMD [ "npm", "start" ]
And as I mentioned I'm using docker-compose, here is the docker-compose.yml file content:
version: "3"
services:
service:
build: .
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3001:3001
command: npm start
After running docker-compose up, I'm facing an error says it's not able to find package.json.
Here is the error:
service_1 | npm ERR! path /usr/src/app/package.json
service_1 | npm ERR! code ENOENT
service_1 | npm ERR! errno -2 service_1 | npm ERR! syscall open
service_1 | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
service_1 | npm ERR! enoent This is related to npm not being able to find a file.
service_1 | npm ERR! enoent
service_1 |
service_1 | npm ERR! A complete log of this run can be found in:
service_1 | npm ERR! /root/.npm/_logs/2019-04-17T07_54_07_773Z-debug.log
xuser-api_service_1 exited with code 254
Please help to find my mistake.
your working directory is /usr/src/app and you copied the package file on root directory .
you have to something like this
# set working directory
WORKDIR /usr/src/app
# install node_modules
ADD package.json /usr/src/app/package.json
RUN npm install
# copy codebase to docker codebase
ADD . /usr/src/app
you may be using an old image which does not contain latest changes.
make sure you using the latest image of your docker file.
docker-compose build
then run
docker-compose up
if you doing frequent changes to Dockerfile for testing then use.
docker-compose up --build
Extend your build section to this:
build:
context: MySuperAngularProject
dockerfile: ./Dockerfile
In context you may set folder with your Angular project with Dockerfile

"npm ERR! enoent ENOENT: no such file or directory" - only happens on 1 computer

I'm using a Windows 10 machine for development at home, a Windows 10 machine for development at work, and an Ubuntu 1604 server for production....
When I run "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up" it always works fine on my Windows 10 development machine at home, but when I run the same command on my Windows 10 machine at work it always throws the error...
database | npm ERR! path /usr/src/app/package.json
database | npm ERR! code ENOENT
database | npm ERR! errno -2
database | npm ERR! syscall open
database | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
database | npm ERR! enoent This is related to npm not being able to find a file.
database | npm ERR! enoent
database |
database | npm ERR! A complete log of this run can be found in:
database | npm ERR! /root/.npm/_logs/2019-01-17T16_28_12_239Z-debug.log
I'm using the exact same code, both on a windows 10 OS! I started using Docker specifically to avoid issues like this and its driving me crazy.
Does anyone know what may be happening???
Dockerfile for my Node app...
FROM node:10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
RUN npm install -g nodemon
# Bundle app source into container
COPY . .
EXPOSE 8080
docker-compose.yml file...
version: "2"
services:
app:
container_name: database
build: .
links:
- mongo
mongo:
container_name: mongo
image: mongo:4
and docker-compose.dev.yml file for running in development....
version: "2"
services:
app:
restart: always
volumes:
- .:/usr/src/app
ports:
- "8080:8080"
command: ["npm", "start"]
mongo:
ports:
- "27017:27017"
I had exactly this problem when running my "docker-compose up" command from WSL bash.
Sharing drive c to Docker and launching via Power Shell fixed my issue.

Error when running node container using docker compose

When running "docker-compose up", I get the following error:
npm info lifecycle server#1.0.0~dev: server#1.0.0
> server#1.0.0 dev /code/app
> nodemon -L ./bin/www --exec babel-node
sh: 0: getcwd() failed: No such file or directory
path.js:1144
cwd = process.cwd();
^
Error: ENOENT: no such file or directory, uv_cwd at Error (native)
at Object.resolve (path.js:1144:25)
at Function.Module._resolveLookupPaths (module.js:361:17)
at Function.Module._resolveFilename (module.js:431:31)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous>
(/usr/local/lib/node_modules/nodemon/bin/nodemon.js:3:11)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
npm info lifecycle server#1.0.0~dev: Failed to exec dev script
npm ERR! Linux 4.9.36-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "dev"
npm ERR! node v6.3.1
npm ERR! npm v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! server#1.0.0 dev: `nodemon -L ./bin/www --exec babel-node`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the server#1.0.0 dev script 'nodemon -L ./bin/www --
exec babel-node'.
My dockerfile looks like this:
FROM joakimbeng/node-zeromq
RUN mkdir /code/
RUN mkdir /code/app/
COPY package.json /code/
WORKDIR /code
RUN npm install -g nodemon babel-cli
RUN npm install
WORKDIR /code/app
CMD ["npm", "run", "dev"]
And my service like this:
node:
build: ./node/
ports:
- "3000:3000"
volumes:
- ../code:/code/app
links:
- mongodb
- python
environment:
- NODE_ENV=dev
- NODE_PATH=/code/node_modules
- MONGODB_ADDRESS=mongodb
- PYTHON_ADDRESS=python
I've tried to delete all containers and images and run the whole thing again, but the same error appears. It seems to build fine when running "docker-compose build".
What I'm trying to accomplish here is:
1. Let the container handle all the dependencies (node modules)
2. Mount my code base to the container
3. Use nodemon for hot reload
I ended up with something similar to what I did initially. Not sure what caused the error in my OP, but the difference seems to be that I mount my dependencies in a different directory.
Dockerfile:
FROM joakimbeng/node-zeromq
RUN mkdir /code/
RUN mkdir /dependencies/
COPY package.json /dependencies/
WORKDIR /dependencies/
RUN npm install -g nodemon babel-cli
npm install
WORKDIR /code/
CMD bash -c "npm run dev"
Service in docker-compose:
node:
build: ./node/
ports:
- "3000:3000"
volumes:
- ../code/:/code
links:
- mongodb
- python
environment:
- NODE_ENV=dev
- NODE_PATH=/dependencies/node_modules
- MONGODB_ADDRESS=mongodb
- PYTHON_ADDRESS=python
This way my dependencies are only installed on build.
Your issue is the volume sharing. When you share a volume from host to the container. If the folder already exists in the container then the host container will shadow the container folder.
If you have 10 files inside container and 0 files on your host, then after volume mapping your container will see 0 files. Because the the host folder is mounted and it has nothing. So you Dockerfile statement
RUN npm install
Is effectively gone, if the host volume doesn't have the npm install done. Luckily the solution is simple. You can change your CMD to below
CMD bash -c "npm install && npm run dev"
In case you don't want to change the Dockerfile you can add the below in your docker-compose.yml file for the node service
command: bash -c "npm install && npm run dev"
Edit (14-Aug):
If you want your dependencies to be in image then you need to make few changes in your docker-compose.yml, what you need is the internal code to be left alone and just linking the node_modules from that directory to a you app directory
node:
build: ./node/
ports:
- "3000:3000"
volumes:
- ../code:/code/app
command: bash -c "ln -fs /code/node_modules /code/app/node_modules && exec npm run dev"
links:
- mongodb
- python
environment:
- NODE_ENV=dev
- NODE_PATH=/code/node_modules
- MONGODB_ADDRESS=mongodb
- PYTHON_ADDRESS=python
Another point i notice is that your running package.json install in /code and putting your code /code/app which is probably wrong when you run the image. But with the new edit I have suggested above, this should work

Resources