skip to Main Content

I’m trying to run an application that requires puppeteer in a docker container and I get the following error when I run the docker container

/usr/src/app/index.js:1
import puppeteer from 'puppeteer';
       ^^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/

My Dockerfile:

FROM ubuntu
WORKDIR /usr/src/app
COPY . .
RUN apt-get update
RUN apt-get install -y imagemagick ghostscript nodejs
RUN apt-get install -y npm
RUN npm ci
CMD ["node","index.js"]

I’m developing this app on a windows machine and the app runs fine on windows but for some reason when either A) Running the app on an ubuntu vm or B) Running the app in a docker container I get this error

2

Answers


  1. Chosen as BEST ANSWER

    The solution to this problem ended up being that installing nodejs through apt-get installs nodejs 10.something.something. I'm using es6 syntax which wasn't stable until 12ish so I had to modify my dockerfile to install the correct version of nodejs


  2. Make sure puppeteer is part of your dependencies in your package.json,

    then try the following instead in your script /usr/src/app/index.js:

    import * as puppeteer from "puppeteer";
    

    as I don’t think the puppeteer package has a puppeteer export.

    The fact that it works on windows with import puppeteer from 'puppeteer'; is a mystery though…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search