Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webstorm—Change require syntax to import syntax with a shortcut

On npmjs.com, libraries often provide code snippets that start like

var md5 = require('md5');

but this syntax is like a million years old. I usually copy and paste the code and transform it to

import md5 from 'md5';

ES2016 style. Is there a shortcut in WebStorm / IntelliJ to do this automatically? Or any other way to not having to do this manually?

like image 847
Lukas Avatar asked Sep 13 '25 11:09

Lukas


1 Answers

IntelliJ 2016.3 & 2017

Starting from IntelliJ 2016.3 you can automatically convert require to import.

See the following youtube demo.

This is also described on the JetBrains blog:

ECMAScript 6. IntelliJ IDEA now reports all var declarations and helps replace them with let or const declarations, depending on recognized value semantics. For all require() calls, the IDE now provides a quick-fix that replaces them with import statements. For function calls and prototype chains, the IDE provides a quick-fix that replaces them with class statements. Other improvements include better support for destructuring assignments, and default exports.

like image 78
ROMANIA_engineer Avatar answered Sep 17 '25 20:09

ROMANIA_engineer