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

On v15 all imports are erroring. #2496

Closed
WilsontheWolf opened this issue Nov 9, 2021 · 10 comments
Closed

On v15 all imports are erroring. #2496

WilsontheWolf opened this issue Nov 9, 2021 · 10 comments

Comments

@WilsontheWolf
Copy link

Hello there. I'm trying to update to v15 of airbnb base. However, upon doing so all of our imports are throwing
Cannot use import declarations in modules that export using CommonJS (module.exports = 'foo' or exports.bar = 'hi') import/no-import-module-exports
I haven't found anything about what is wrong in my code. If I could get some insight into what I'm doing wrong that would be greatly appreciated. Here is some info.

.eslintrc.json

{
    "env": {
        "es2021": true,
        "node": true
    },
    "extends": ["airbnb-base", "prettier"],
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "rules": {
        "no-console": "off",
        "no-param-reassign": "off",
        "no-restricted-syntax": "off"
    }
}

Eslint version ^8.2.0

A screenshot of the error
image

@ljharb
Copy link
Collaborator

ljharb commented Nov 9, 2021

Are you using module.exports = in that file? (can you share the entire contents of the file, as text?)

@WilsontheWolf
Copy link
Author

WilsontheWolf commented Nov 9, 2021

Sure. Here Is one of the files

import fs from 'fs/promises';

const subscriptions = new Map();

export default async (client) => {
    /**
     * loads all modules and their subscriptions
     */
    const modules = await fs.readdir('./src/modules');

    await Promise.all(
        modules.map(async (moduleName) => {
            // Loads the module
            const module = await import(`./modules/${moduleName}/module.js`);
            // skips the module, in case it is disabled.
            if (module.enabled) {
                // Loads each of it's subscriptions into their according list.
                module.subscriptions.forEach((fun, event) => {
                    if (!subscriptions.has(event)) {
                        subscriptions.set(event, []);
                    }
                    subscriptions.get(event).push(fun);
                });
            }
        })
    );

    /**
     * Setting up all events.
     * binds all events inside the subscriptions map to call all functions provided
     */
    subscriptions.forEach((funs, event) => {
        client.on(event, (...args) => {
            funs.forEach(async (fun) => {
                try {
                    await fun(client, ...args);
                } catch (e) {
                    client.emit('error', e);
                }
            });
        });
    });
};

I just noticed it isn't all files, but actually just ones I import fs/promises in.

@ljharb
Copy link
Collaborator

ljharb commented Nov 9, 2021

Seems like I can reproduce this; looking into it.

Could you file an issue for this (with the same content) on eslint-plugin-import directly?

In the meantime, you can disable the rule in your eslint config to avoid the errors.

@WilsontheWolf
Copy link
Author

Yeah I can do that. Thanks.

@ljharb
Copy link
Collaborator

ljharb commented Nov 9, 2021

The issue seems to be when you've named a variable module or exports, and also have at least one import statement present.

It's a bug in eslint-plugin-import; we'll try to resolve it there.

@hasnaindev
Copy link

@ljharb Was this issue resolved? I'm using WebPack along with HMR where WebPack provides a "module" variable. I looked at your comment and removed this line from code and the error vanished.

if (module && module.hot) module.hot.accept();

I took me several hours to get here and realize what exactly the issue was. I'm using the latest packages and the issue is still there. Thank you!

@ljharb
Copy link
Collaborator

ljharb commented Feb 20, 2022

@hasnaindev if it’s still there in v2.25.3 or later of the import plugin, please file a new issue there.

@jt3k
Copy link

jt3k commented Feb 14, 2023

bug is still present. reopen

@ljharb
Copy link
Collaborator

ljharb commented Feb 14, 2023

@jt3k no, but you can file a new issue.

@hasnaindev
Copy link

hasnaindev commented Feb 15, 2023

Hello @jt3k 👋🏼

I looked over at the eslint-plugin-import source code and I can see that for the rule no-import-module-exports, they're just looking at two identifiers to be present in the same file, which are, export and module. So, if you're using ESM and at the same time, one of your variable is called, module, it's going to mark it as an error even if it's not in the context of CJS.

A solution for now would be to avoid using variable module in a file that's also using ESM. "It doesn't matter if module is not related to CommonJS." You can look into this file and see how the rule is implemented for yourself. no-import-module-exports.

As @ljharb suggested, a good idea would be to open an issue in this repo: eslint-plugin-import. I hope this helps.

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

No branches or pull requests

4 participants