Auto import works for ES6 modules but not for CommonJS modules.

To explain this problem I created 2 modules

A CommonJS module


const myFuncA = () => {}
exports.myFuncA = myFuncA;


And an ES6 module


const myFuncB = () => {}
export default myFuncB;


Then in another file c.js and I use autocomplete for myFuncB an ES6 module from b.js, I get it auto imported

import myFuncB from 'modules/b';
const c = myFuncB();


But when I autocomplete a myFuncA a CommonJS module from a.js, it is not auto imported. While I expect it to be auto imported as a CommonJS module

const myFuncA = require('modules/a')

Any ideas why? 


Also I don't have auto suggestions for Node core modules (ie fs, http). Though when a core module is imported in the file, I get auto suggestions for its methods (ie fs.readFile, http.request). 

I have set NODE_PATH=app 

Node version 12.10

./app directory is marked as resource root, and use of paths to resource root is enabled

Tried on WebStorm 2019.2.2 latest build and 2019.3 EAP latest build


Comment actions Permalink

Auto-import only works for imports, not for require() calls

But you can add a missing require() via Alt+Enter:

Comment actions Permalink

> But you can add a missing require() via Alt+Enter:

I don't have this option

Is there a way it can be enabled? Check for "require statements in Node.js" is enabled in inspections.

It feels like WebStorm treats my js files as front-end code only. Everything works except Node.js relates stuff: CommonJS modules, Node Core modules suggestions etc.

Comment actions Permalink

Please make sure that Coding assistance for Node.js is enabled in Settings | Languages & Frameworks | Node.js and NPM


Comment actions Permalink

Yes, it was always enabled. 

Comment actions Permalink

please share a project the issue can be reproduced with

Comment actions Permalink

I'm experiencing the same problem (since always).

This happens when a class or function is exported directly:

module.exports = DummyMysqlRepository;

But if I wrap it like so the autocomplete works fine:

module.exports = { DummyMysqlRepository };

Which is of course not an option in existing code bases.

Elena Pogorelova

Comment actions Permalink

Thanks, reproduced. Submitted to developers as, please follow it for updates


Please sign in to leave a comment.