Search code examples
node.jsimportwebstorm

How can I automatically 'audit' or 'rationalise' my imports in a NodeJS project?


I'd like to be able to go through my project and automatically change all instances in which an entire library is imported when only one export of that library is needed to only import the export(s) needed. Thus

import R from 'ramda'

const sillyValue = R.add(2,3)

const moreSilly = R.always('foo')

would be changed to

import {add, always} from 'ramda'

const sillyValue = add(2,3)

const moreSilly = always('foo')

Is there any sort of utility that I can run to do this automatically for all the modules in my project at once? I have WebStorm, but so far I haven't found any way to do it using the IDE ('Optimize Imports', while nice, doesn't seem to do what I want).


Solution

  • WebStorm doesn't have such inspection; you can try using ESLint for this, see eslint-plugin-import plugin, no-named-as-default-member rule