Must use import to load ES Module

I am attempting to use the Slack example found here Actions Triggers: post-user-registration for a post registration Action. However, when I use the exact code I get “Must use import to load ES Module” error from Node.JS. When I follow the error instructions and use import I get “Cannot use import statement outside a module”. So I fell back to using the new dynamic import method await import('slack-notify') but that results in “no callback provided in dynamic import”. Has anyone seen this type of error? I triple checked my module and secrets configuration.

This is using Node16 for a post-registration Action.

1 Like

Hi @jphillips,

Thanks for reaching out.

I am running into the same error. Can you try switching to the @slack/webhook - npm library?

Something like this:

/**
 * @param {Event} event - Details about newly created user.
 */

exports.onExecutePostUserRegistration = async (event) => {
  const { IncomingWebhook } = require('@slack/webhook');
  const url = event.secrets.SLACK_WEBHOOK_URL;

  const webhook = new IncomingWebhook(url);

  // Send the notification
  (async () => {
    await webhook.send({
      text: 'I\'ve got news for you...',
    });
  })();

};

This worked, thanks so much for the solution!

1 Like

Now how can I pass the channel name using slack webhook?

The documentation on Auth0 shows the way to use require(‘slack-notify’)… but when we add slack-notify in modules, it fails and gives the “Must use import to load ES Module” error.

const slack = require(‘slack-notify’)(event.secrets.SLACK_WEBHOOK_URL);

let referrer_path = '';

const message = `New User: ${event.user.email} Came from ${JSON.stringify(api)} with the following ${JSON.stringify(event.request)}  and the entire event contains ${JSON.stringify(event)}`;

const channel = '#marcoms';

slack.success({

 text: message,

 channel: channel

});

I don’t see that as an option in the slack/webhook library. If this is something you need, can you please elaborate on the use case?

The solution you are looking for is below, I needed to specify a channel as well so I had to poke around the code a bit.

` ```
const { IncomingWebhook } = require('@slack/webhook');
  const url = event.secrets.SLACK_WEBHOOK_URL;

  const webhook = new IncomingWebhook(url);

  // Send the notification
  (async () => {
    await webhook.send({
      text: 'I\'ve got news for you...',
      channel: '#marcoms'
    });
  })();
1 Like

Thanks for providing that code @jphillips!

Hi @dan.woda

Im running into the same issue (and very new to Auth0!) :sweat_smile:

This Is my current code

exports.onExecutePostUserRegistration = async (event) => {
  const slack = require('slack-notify')(event.secrets.SLACK_WEBHOOK_URL);

  const message = `New User: ${event.user.email}`;
  const channel = '#ct_new_user_notifications'

  slack.success({
   text: message,
   channel: channel
  });
};

I have a module called slack-notify@latest

Please could you advise on getting this to work? Ive included a screenshot of the error when I test the action.

Many many thanks!

Hi @Senor_Fiorano,

Welcome to Auth0!

The solution is stated directly in this thread, and is marked with “:white_check_mark: Solution” . Is there some part of it that is not clear?

Hi @dan.woda
Thanks for the prompt reply - much appreciated.

Due to my ‘noob-ness’ I incorrectly configured the ‘@slack/webhook’ module. All working as expected. Out of interest are you able to explain why the documentation I followed wont work?

Thanks again,

Fiorano

1 Like

@Senor_Fiorano

I don’t have an explanation as to why that library is throwing the error, but we are actively working to update the documentation.

1 Like