Question

Nodejs Function import module error

I have been trying for the last 5 days to get a simple function to work. Not even the demo examples work and at this point, I think the issue is with DigitalOcean. I am using a Nodejs function to output a string. I try using import a module and this is the error I get: " Error [ERR_MODULE_NOT_FOUND]: Cannot find package ‘parse-domain’ imported from /tmp/index.mjs". So I try to change it to require and get this error: “ReferenceError: require is not defined in ES module scope, you can use import instead”. I am using “doctl serverless deploy . --remote-build” in the same directory as the project.yml. I added “type”: “module” in the package.json as well. I have the dependencies listed in there as well. I also ran npm install --package-lock-only parse-domain which didn’t work. I have module.exports.main = checkURL (function name) at the end. There is little to no documentation around DO functions and nodejs other than the examples which again doesn’t even work because I get the same error.


Submit an answer
Answer a question...

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.

Accepted Answer

Hey Bobby,

Thanks for responding! I did confirm doctl is up to date. The API token I’m using has both read and write permissions. Here is the repo I am using to test: https://github.com/SevenGuru/DO_Functions.

If you do come across something please let me know!

Bobby Iliev
Site Moderator
Site Moderator badge
February 25, 2023

Hi there,

I’ve just tried deploying one of the example Node.js serverless functions and it seems to be working as expected.

Can you confirm the following:

  • Make sure you have the latest version of doctl installed on your local machine.
  • Verify that your API token has write permissions so that the deployment could work as intended.
  • Can you share the repo containing your serverless function so I could try to have a look and identify any issues that might be causing the problem?

Best,

Bobby

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.