Npm ERR! enoent Could not read package.json: Error: ENOENT: no such fi le or directory, open '/package.json'

Hello,
My YAML file is as follows:

version: '3.9'
services:
  web:
    image: nginx:latest
    container_name: Nginx
    ports:
      - '8080:80'
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - /var/www/html:/usr/share/nginx/html
    depends_on:
       - php-fpm
    links:
       - php-fpm
       - nodejs

  php-fpm:
    image: php:8-fpm
    container_name: PHP
    volumes:
       - /var/www/html:/usr/share/nginx/html/

  nodejs:
    image: node:latest
    container_name: Nodejs
    command:
       - bash
       - -c
       - |
         npm install
         npm start
    volumes:
       - /var/www/html:/usr/src/app

The default.conf file is as follows:

server {
   listen 80;
   server_name default_server;
   error_log  /var/log/nginx/error.system-default.log;
   access_log /var/log/nginx/access.system-default.log;
   charset utf-8;
    root /usr/share/nginx/html;
    index index.html index.php index.js;
    location ~ \.php$ {
        fastcgi_pass    php-fpm:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        add_header      script-filename $document_root$fastcgi_script_name alway
s;
        include         fastcgi_params;
    }
location ~ \.js$ {
    proxy_pass http://nodejs:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
    location / {
     autoindex on;
    try_files $uri $uri/ $uri.html =404;
    }
}

I have copied two files index.js and package.json to /var/www/html directory. The contents of the index.js file is as follows:

const http = require('http')
const server = http.createServer((req, res) => {
	res.writeHead(200, { 'content-type': 'text/html' })

	if (req.url === '/') {
		res.write('<h1>Node and Nginx on Docker is Working</h1>')
		res.end()
	} else {
		res.write('<h1>404 Nout Found</h1>')
		res.end()
	}
})

server.listen(process.env.PORT || 3000, () => console.log(`server running on ${server.address().port}`))

The contents of the package.json file is as follows:

{
  "name": "node-nginx",
  "version": "0.0.1",
  "description": "node with nginx on docker for beginner",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": ["node","nginx","docker","ingress"],
  "author": "restuwahyu13",
  "license": "MIT"
}

After running the containers, I got the following errors:

Nodejs  | npm ERR! Tracker "idealTree" already exists
Nodejs  |
Nodejs  | npm ERR! A complete log of this run can be found in: /root/.npm/_logs/
2024-04-01T13_41_35_166Z-debug-0.log
Nodejs  | npm ERR! code ENOENT
Nodejs  | npm ERR! syscall open
Nodejs  | npm ERR! path /package.json
Nodejs  | npm ERR! errno -2
Nodejs  | npm ERR! enoent Could not read package.json: Error: ENOENT: no such fi
le or directory, open '/package.json'
Nodejs  | npm ERR! enoent This is related to npm not being able to find a file.
Nodejs  | npm ERR! enoent
Nodejs  |
Nodejs  | npm ERR! A complete log of this run can be found in: /root/.npm/_logs/
2024-04-01T13_41_36_118Z-debug-0.log

What is wrong?

Cheers.

What’s wrong? You are throwing together multiple containers, multiple config files, all in the same folder, there seems to be no structure.

Containers are for isolation.

Use a NodeJS container for NodeJS, use a separate folder for the necessary files, check a matching tutorial (1, 2, 3).

You are very welcome for further questions if those don’t work for you.

Hi,
Thank you so much for your reply and useful links.
I also found this link.
Why not multiple containers? Is this odd? I guess this is the art of Docker.
I need a web server to run NodeJS applications.

Of course multiple containers. But not use the same /var/www/html in each of them.

Why can’t it see the package.json file?

Because you use a bad config. It seems you try to do everything but don’t understand the details yet.

You use again /var/www/html, which seems to include nginx config, html files, php files and NodeJS files.

This is dangerous because some container could leak the files, as in allow plain text access. And you don’t want anyone to see stuff like your database connection credentials.

You assume /usr/src/app is the workdir of the NodeJS container. Maybe set a workdir in config or cd into it before running the commands.

Explain what you want to do. Should that be a development container or a production container, as they will usually not have the same Dockerfile.

Example NodeJS production Dockerfile:

FROM node:18-alpine

# set context
EXPOSE 3001
WORKDIR /home/node/app

# install dependencies
COPY package*.json ./
RUN npm ci

# add source to container
COPY dist ./

# run server
CMD [ "node", "server.js" ]

For development you probably better use a bind mount for the source files and something like nodemon to restart node upon file changes. For dependency changes (added npm lib) you still need to restart the container.

1 Like

Hello,
Thanks again. I want to have a Nginx, NodeJS and a PHP(later) containers.
I edited the Dockerfile as follows:

FROM node:latest

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN npm install
RUN npm update

COPY ./ /usr/src/app

EXPOSE 3000

CMD npm run

I have a package.json and index.js file in the current directory as follows:

{
  "name": "docker-nodejs",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "prettify": "prettier -l --write \"**/*.js\"",
    "test": "jest",
    "dev": "nodemon --inspect=0.0.0.0:9229 -L /usr/src/app/src/index.js"
  },
  "dependencies": {
    "express": "^4.18.2",
    "pg": "^8.11.2",
    "sqlite3": "^5.1.2",
    "uuid": "^9.0.0",
    "wait-port": "^1.0.4"
  },
  "resolutions": {
    "ansi-regex": "5.0.1"
  },
  "prettier": {
    "trailingComma": "all",
    "tabWidth": 4,
    "useTabs": false,
    "semi": true,
    "singleQuote": true
  },
  "devDependencies": {
    "jest": "^29.6.2",
    "nodemon": "^3.0.1",
    "prettier": "^2.7.1"
  }
}

And:

const http = require('http')
const server = http.createServer((req, res) => {
	res.writeHead(200, { 'content-type': 'text/html' })

	if (req.url === '/') {
		res.write('<h1>Node and Nginx on Docker is Working</h1>')
		res.end()
	} else {
		res.write('<h1>404 Nout Found</h1>')
		res.end()
	}
})

server.listen(process.env.PORT || 3000, () => console.log(`server running on ${server.address().port}`))

The YAML file is as follows:

version: '3.9'
services:
    nodejs:
      container_name: Node
      build:
         context: .
         dockerfile: Dockerfile
      expose:
        - "3000"

I just want to run NodeJS for now. I got the following error:

Attaching to Node
Node  | npm ERR! Missing script: "start"
Node  | npm ERR! 
Node  | npm ERR! Did you mean one of these?
Node  | npm ERR!     npm star # Mark your favorite packages
Node  | npm ERR!     npm stars # View packages marked as favorites
Node  | npm ERR! 
Node  | npm ERR! To see a list of scripts, run:
Node  | npm ERR!   npm run
Node  | 
Node  | npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2024-04-02T14_08_57_119Z-debug-0.log
Node exited with code 1

As the error says: package.json has no script with the name start.

1 Like

Hi,
Where should I add start?

If you want to chat for solutions, instead of reading any basic docs/tutorials, then you maybe try ChatGPT/Gemini or similar.

Thanks.
I just copy and paste it from the internet. Why did it work there and not here!

I changed the code as below:

"scripts": {
    "prettify": "prettier -l --write \"**/*.js\"",
    "test": "jest",
    "dev": "nodemoon --inspect=0.0.0.0:9229 -L ./usr/src/app/src/index.js",
    "start": "node ./usr/src/app/src/index.js"
  }

Not worked!

Hi,
Problem solved.
I created a www directory and moved package.json and index.js into it.
I modified the package.json file as follows:

{
  "name": "nodejs-app",
  "dependencies": {
    "express": "^4.17.0"
  },
  "scripts": {
    "start": "node index.js"
  }
}

I also modified the Dockerfile as below:

FROM node:latest

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY ./www/package.json /usr/src/app/package.json
RUN npm install
RUN npm update

COPY ./www /usr/src/app
EXPOSE 3000

The YAML file is as follows:

version: '3.9'
services:
    nodejs:
      container_name: Node
      build:
         context: .
         dockerfile: Dockerfile
      command: npm start
      volumes:
        - ./www:/usr/src/app
      expose:
        - "3000"

Finally:

Node  |
Node  | > docker-nodejs@1.0.0 start
Node  | > node index.js
Node  |
Node  | server running on 3000