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:

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 (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.

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: