Skip to content

Prevent all Node.js modules when not detected to be in a Node environment #321

Open
@brettz9

Description

@brettz9

I would like to propose a rule which forbids all Node.js core modules, though which makes allowances for them under certain conditions. The purpose would be to ensure that one restricts such modules to use within Node entry files but otherwise allows one's code to work universally as polyglot code.

As far as the conditions under which to allow Node.js modules:

  1. The file is pointed to by a node conditional export in package.json
  2. The file has {env: {node: true}} applied to it

These conditions are more likely to suggest that the file or the entire package is Node only.

Such an option ought to help discourage packages which could be polyglot friendly from restricting their files or packages unnecessarily to Node only.

If you are open to the idea, I would think this could be its own rule, or it could be added as an option within the likes of node/no-restricted-import/node/no-restricted-require.

(Note that I originally submitted this rule proposal to import-js/eslint-plugin-import#2369 as that project already has a rule to restrict Node modules, but as they did not wish to support this, I thought I would ask here, especially since you already have some other very convenient rules which check package.json or other config.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions