0

This is my Dockerfile:

FROM node:12
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

This is the npm script in my package.json

"start": "main.js $npm_config_e $npm_config_t"

when I run the docker image I get this error

sh: 1: main.js: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! [email protected] start: `main.js`

However when I do an 'ls' the main.js is in the working directory (/app) also if I change the Dockerfile to:

CMD ["node", "main.js"]

it works. So why does the npm script not find the main.js file?

4
  • How are you running the container? Is the main.js file executable with a #! interpreter line at the start? In your second invocation when you claim it works, what output do you actually get? (Single quotes in JSON-format CMD aren't allowed and are a common cause of an obscure shell error at startup time.)
    – David Maze
    Commented Oct 16, 2020 at 10:56
  • I have not added #! at the start of main.js, is this required for running an npm script in docker? Using CMD ["node", "main.js"] (the single quotes were a typo sorry) I get my expected output from Main.js. again without #! Thanks
    – Blobafet
    Commented Oct 16, 2020 at 12:47
  • If you want to directly run a script ./main.js, it either needs to be a native executable or begin with the #! marker. (Not Docker-specific.)
    – David Maze
    Commented Oct 16, 2020 at 13:21
  • Sadly this didn't make a difference. Thanks
    – Blobafet
    Commented Oct 16, 2020 at 15:19

1 Answer 1

0

Seems obvious now but the solution was to add node to the npm script:

"start": "node main.js $npm_config_e $npm_config_t"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.