JavaScript

JavaScript

Made by DeepSource

Module imported is not necessary JS-0234

Performance
Minor

Imports are an ES6/ES2015 standard for making the functionality of other modules available in your current module. In CommonJS this is implemented through the require() call.

Why would you want to restrict imports?

  • Some imports might not make sense in a particular environment. For example, Node.js' fs module would not make sense in an environment that didn't have a file system.

  • Some modules provide similar or identical functionality, think lodash and underscore. Your project may have standardized on a module. You want to make sure that the other alternatives are not being used as this would unnecessarily bloat the project and provide a higher maintenance cost of two dependencies when one would suffice.

Bad Practice

import fs from 'fs';

export { fs } from 'fs';

export * from 'fs';

import cluster from 'cluster';

import pick from 'lodash/pick';

import DisallowedObject from "foo";

import { DisallowedObject as AllowedObject } from "foo";

import * as Foo from "foo";

Recommended

import crypto from 'crypto';
export { foo } from "bar";

import crypto from 'crypto';
import eslint from 'eslint';
export * from "path";

import DisallowedObject from "foo"

import { AllowedObject as DisallowedObject } from "foo";