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

To explain this problem I created 2 modules

A CommonJS module

app/modules/a.js

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

 

And an ES6 module

app/modules/b.js

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

 

5 comments
Comment actions Permalink

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

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

0
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.

0
Comment actions Permalink

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

 

0
Comment actions Permalink

Yes, it was always enabled. 

0
Comment actions Permalink

please share a project the issue can be reproduced with

0

Please sign in to leave a comment.