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

import/no-import-module-exports is triggered on all imports when a variable named module is used. #2297

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

Comments

@WilsontheWolf
Copy link

Hello there. I've been updating dependancies and now in all files I import fs/promises in I am getting Cannot use import declarations in modules that export using CommonJS (module.exports = 'foo' or exports.bar = 'hi') import/no-import-module-exports

I'm not entirely sure whats happening here.

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
Eslint plug import version ^2.25.2

An example file that is getting the error:

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);
                }
            });
        });
    });
};

A screenshot of the error happening
image

A related issue from eslint-config-airbnb-base
airbnb/javascript#2496

WilsontheWolf added a commit to IIoW/IIslander that referenced this issue Nov 9, 2021
This updates eslint and airbnb eslint.
I also upgraded husky cause why not.
Resolved some new issues caused by airbnb upgrade.
Disabled a broken rule. See import-js/eslint-plugin-import#2297

closes #99
closes #100
WilsontheWolf added a commit to IIoW/IIslander that referenced this issue Nov 9, 2021
This updates eslint and airbnb eslint.
I also upgraded husky cause why not.
Resolved some new issues caused by airbnb upgrade.
Disabled a broken rule. See import-js/eslint-plugin-import#2297

closes #99
closes #100
@ljharb
Copy link
Member

ljharb commented Nov 9, 2021

Specifically, it's two things that are triggering the false positive - the presence of at least one import declaration, and the module.enabled member expression (and probably module.subscriptions would trigger it too).

I'm looking into if there's an easy way to confirm whether module or exports are the top-level ones, or are shadowed like you've done in your code.

@WilsontheWolf
Copy link
Author

Oh, that's the issue. Yeah, I guess that is kinda my fault then. Not entirely sure the best way to handle this but thanks for taking a look at it.

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

No branches or pull requests

2 participants