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

BUG : Clear-module doesn't skip native module #18

Open
Fadoli opened this issue Apr 7, 2021 · 1 comment
Open

BUG : Clear-module doesn't skip native module #18

Fadoli opened this issue Apr 7, 2021 · 1 comment

Comments

@Fadoli
Copy link

Fadoli commented Apr 7, 2021

Hey we've been using this module a bit, and I've encountered an issue with it.

It seems you don't filter out native module, and as per nodejs documentation this causes bad behavior when re-requiring code later on.

This documentation as been added there : nodejs/node@5c14d69

To reproduce, clear-module and reimport any library that internally uses a native library ( '.node' file )

I'm getting an Error: Unknown failure when requiring it back and more specifically when the codes tries to call a function (that's within the .node )

@Fadoli
Copy link
Author

Fadoli commented Apr 7, 2021

from pocking around (in node 14), it seems like we can't properly clean what requires it, a safer approach would be to skip the module whose child is a .node and all its childrens to prevent breaking anything to weirdly.

    // Remove all descendants from cache as well
    if (require.cache[filePath]) {
        const { children } = require.cache[filePath];

        let isSafe = true;
        for (const { id } of children) {
            if (id.endsWith('.node')) {
                isSafe = false;
            }
        }

        if (isSafe) {
            delete require.cache[filePath];

            for (const { id } of children) {
                clear(id);
            }
        }
    }

I don't know if that would impact anyone else's usage.

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

2 participants