Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ESM #772

Open
its-dibo opened this issue Feb 8, 2023 · 8 comments
Open

support ESM #772

its-dibo opened this issue Feb 8, 2023 · 8 comments

Comments

@its-dibo
Copy link

its-dibo commented Feb 8, 2023

I want to use web-push server functions i.e. generateVAPIDKeys and sendNotification, but it seems that web-push doesn't support esm packages, i.e packages that define type: module on its package.json

SyntaxError: Named export 'generateVAPIDKeys' not found. The requested module 'web-push' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

when using the default export the same error occurs because the file is compiled into ESM

@MumiaIrrequieta
Copy link

Try this @eng-dibo :
`import webpush from 'web-push'

webpush.sendNotification(...)`

@marco-c
Copy link
Member

marco-c commented Nov 21, 2023

I'd be happy to accept a PR if anybody wants to help. It sounds relatively easy to move to ESM (see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-move-my-commonjs-project-to-esm).

@senbar
Copy link

senbar commented Jan 6, 2024

I migrated everything to ESM but have one problem with tests namely testSendNotification.js had before each

 mocha.beforeEach(function () {
    ({ sendNotification, setGCMAPIKey, setVapidDetails } = require('../src/index'));
  });

and then it deleted cache before each for clean start

  mocha.beforeEach(function() {
    requestBody = null;
    requestDetails = null;

    // Delete caches of web push libs to start clean between test runs
    delete require.cache[path.join(__dirname, '..', 'src', 'index.js')];
    delete require.cache[path.join(__dirname, '..', 'src', 'web-push-lib.js')];

How would you recommend me approaching this if we wnat to get rid of require to make it ESM?

@marco-c
Copy link
Member

marco-c commented Jan 15, 2024

I'm not sure to be honest, I'm not familiar enough with ESM.

@senbar
Copy link

senbar commented Jan 16, 2024

Would you be ok with me adding clearCache method to web-push-lib.js and index.js that would clear all state for testing? I don't think there's other way around that

@marco-c
Copy link
Member

marco-c commented Jan 16, 2024

What if we simply call setGCMAPIKey and setVapidDetails with null as a way to reset the state?

@nikeee
Copy link
Contributor

nikeee commented May 12, 2024

Maybe if #876 is done first, the need to reset state vanishes.

@khanyasar022
Copy link

I migrated everything to ESM but have one problem with tests namely testSendNotification.js had before each

 mocha.beforeEach(function () {
    ({ sendNotification, setGCMAPIKey, setVapidDetails } = require('../src/index'));
  });

and then it deleted cache before each for clean start

  mocha.beforeEach(function() {
    requestBody = null;
    requestDetails = null;

    // Delete caches of web push libs to start clean between test runs
    delete require.cache[path.join(__dirname, '..', 'src', 'index.js')];
    delete require.cache[path.join(__dirname, '..', 'src', 'web-push-lib.js')];

How would you recommend me approaching this if we wnat to get rid of require to make it ESM?

async function getFreshModule() {
    const modulePath = path.resolve(fileURLToPath(new URL('./module.js', import.meta.url)));
    const { default: module } = await import(`${modulePath}?t=${Date.now()}`);
    return module;
}

beforeEach(async function() {
    const freshModule = await getFreshModule();
});

try this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants