Docker Community Forums

Share and learn in the Docker community.

Can't run the "npm init" command from the Dockerfile build?

I tried to run the RUN npm init command from the Dockerfile during the docker build but this returned the following: “The command '/ bin / sh -c npm init' returned a non-zero code: 1

It’s not possible? Should I always create an external package.json file and run npm install?

I thank you in advance for those who are willing to take this doubt away from me.

The error message means that npm can’t find a package.json file which includes a script named start. So make sure that your package.json contains a script named start! Might already be the issue.

// Simple package.json example that contains a script with name test.
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”
When you look at the tutorial’s dockerfile:

This makes sure that the commands are executed in a specific folder, e.g. copying files:

Set the working directory.

WORKDIR /usr/src/app
This makes sure that the package.json file is copied into the workdir:

Copy the file from your host to your current location.

COPY package.json .
Here it defines that npm start will be executed when you run the container:

Run the specified command within the container.

CMD [ “npm”, “start” ]
To further investigate I would execute the following command to start the container:

docker run -it -d /bin/sh
Then go to the workdir and check if the app can be started manually:

Go to workdir

cd /usr/src/app

Here check if the package.json is part of the directory.


Manually run npm script

npm start

First, I would like to thank you for your response.

So, I tried to run the command npm init just to create the file packge.json.

Apparently, Docker couldn’t execute npm init command.