Question

Why do I get "require is not defined in ES module scope, you can use import instead" error during tutorial

Hi, I’m trying to follow the tutorial:

https://docs.digitalocean.com/tutorials/jamstack-series/

Everything seemed to move along just fine, until I test the cloud/getCoffee function. When I do that, I get the above error.

I have to add though, I don’t get it 100% of the time. For a while, it appeared that if I modify (like adding/removing a blank line) & save the function form the Digital Ocean page, it would work, but as soon as I deploy it from doctl, I’d get the error again. That was a couple hours ago. Now that I’m back at it, I can’t get it working properly at all. I suspect if I keep goofing with it though, I may get some intermittent success. I haven’t nailed down the specifics to reliably reproduce the problem.

I have a scripting background, some minor web app development, but no experience with NodeJS/Mongo/Cloud. That’s why I’m messing with the tutorial.

Any ideas?

Thank you

Activation Id: 32a38503a25641e6a38503a25651e600
2022-09-18T17:48:44.171377073Z stdout: ReferenceError: require is not defined in ES module scope, you can use import instead
2022-09-18T17:48:44.171455712Z stdout:     at file:///tmp/index.mjs:2:21
2022-09-18T17:48:44.171463273Z stdout:     at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
2022-09-18T17:48:44.171467211Z stdout:     at async Loader.import (internal/modules/esm/loader.js:178:24)
2022-09-18T17:48:44.171470901Z stdout:     at async actionLoop (/nodejsAction/prelauncher.js:68:20)
2022-09-18T17:48:44.174519Z    stderr: The action did not initialize or run as expected. Log data might be missing.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I was able to fix this by changing the function’s runtime from Node 14 (the current default) to Node 18.

You can do that by defining nodejs:18 as the function’s runtime:

parameters: {}
environment: {}
packages:
  - name: sample
    shared: false
    environment: {}
    parameters: {}
    annotations: {}
    functions:
      - name: hello
        binary: false
        main: ""
        runtime: nodejs:18
        web: true
        websecure: false
        parameters: {}
        environment: {}
        annotations: {}
        limits: {}

You can also do it in the web interface of the function’s settings, but if you deploy again from the commandline this is overwritten with what’s defined in package.yml:

  1. Go to the function in DigitalOcean’s dashboard
  2. Click on “Settings”
  3. Under “Runtime” click Edit, to change to Node 18.

Here are the docs on available runtimes: https://docs.digitalocean.com/products/functions/reference/runtimes/

Bobby Iliev
Site Moderator
Site Moderator badge
November 8, 2022

Hi there,

Does it work with doctl if you try to add the --remote-build flag?

As a reference, the URL to the tutorial has been updated to:

https://docs.digitalocean.com/tutorials/jamstack-series-part-2-add-serverless-functions-to-app/

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up