Must use import to load ES Module

Hi, suddenly my workflow started to fail with the following error: “Must use import to load ES Module”.

I know there’s a similar issue but when I change require to import it tells me the following:

'import' and 'export' may only appear at the top level

@germanescobar you can give this a try: const { default: fetch} = await import('node-fetch').

Here is a relevant thread with additional context in case you’re interested: ErrorMust use import to load ES Module when deploying Pipedream workflow.

Thanks @droosevelt, I was trying with static imports instead of dynamic ones. Just in case someone else encounters this issue, it worked with the following dynamic imports:

const { default: fetch } = await import('node-fetch')
const { default: AWS } = await import("aws-sdk")
const { format } = await import('date-fns')

The weird thing is that I have other steps that work well using require, do you have an explanation of why do we need dynamic imports here?

@germanescobar workflows run as AWS Lambda scripts behind the scenes: we run a custom execution environment within the Lambda environment, and rely on the AWS Node 14 runtime to execute your code.

AWS Lambda currently doesn’t support ESM imports, so you’ve got to use dynamic imports / CommonJS requires (where supported) for all imports today.

We’re planning to address this in better ways in the future (e.g. by letting you pin package versions), and we plan to allow static ESM imports when the AWS runtime supports it.

Let me know if that helps.

1 Like

For me, it looks like an npm package is causing the error message. I replaced the ‘require’ code in the workflow, but still get “Must use import …” for fetch.

const { default: fetch } = await import('node-fetch');
const { default: FMData } = await import('fm-data-api-client');

When I comment out the ‘fm-data-api-client’ line, I get the following error.

ErrorCannot find module 'node-fetch' Require stack: - /tmp/ee/node_modules/fm-data-api-client/lib/Client.js - /tmp/ee/node_modules/fm-data-api-client/lib/index.js

Here’s their reference to fetch in Client.js. No references in index.js.

import fetch, {Headers, RequestInit} from 'node-fetch';

Is there another way I can resolve this? Thanks!

Hi @pegasuslegal , would you mind clicking the Share button at the top-right of your workflow and share it with dylan@pipedream.com?

Also, do you mind if I modify the code to test something?

I’m not able to reproduce the error in a workflow with just that one line:

const { default: fetch } = await import('node-fetch')

so I’d like to see if there’s something else awry.

Thanks so much, I’ve shared it with you. I feel like all is well in the workflow and your documentation is invaluable.

I believe it’s the way the ‘fm-data-api-client’ npm package is trying to load fetch itself. I forked their project, but looks like creating a valid npm package is above what I can do right now. I have an editable one at ‘Lab548/fm-data-api-client’ but not shared with npm.

This is a new workflow, but I have 5-6 others across two accounts that are live and use the same code, so trying to get ahead of them failing.

I did look at what’s going on in the ‘node-fetch’ GitHub page, so totally get this is coming from elsewhere and there’s apparently a good deal of controversy. :grinning:

Thanks!

@pegasuslegal in this case, I would recommend trying to publish an npm package from your fork. I actually just did the same thing and got it working, so I’ll walk you through what I did.

  1. Edit the name of the package.json to @<your npm username>/fm-data-api-client. For example, my npm username is @dylburger, so I’ve changed it to @dylburger/fm-data-api-client.
  2. Move everything in peerDependencies to dependencies (see my package.json).
  3. Take a look at our docs on publishing your own npm package

You can try using my package, too.

Let me know if that helps.

@dylburger I can’t thank you enough.

I’m green on npm sharing, so wasn’t sure on the extent of package.json editing needed and this is awesome. I’ll use yours as a one example and get one published.

Really appreciate it!

I’ve been meaning to get back to you and Tod on what I’ve been using Pipedream for and will be sure to do that this week.

1 Like