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
Please sign in to leave a comment.
Auto-import only works for imports, not for require() calls
But you can add a missing require() via Alt+Enter:
> 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.
Please make sure that Coding assistance for Node.js is enabled in Settings | Languages & Frameworks | Node.js and NPM
Yes, it was always enabled.
please share a project the issue can be reproduced with
I'm experiencing the same problem (since always).
This happens when a class or function is exported directly:
But if I wrap it like so the autocomplete works fine:
Which is of course not an option in existing code bases.
Elena Pogorelova
Thanks, reproduced. Submitted to developers as https://youtrack.jetbrains.com/issue/WEB-42566, please follow it for updates
I apologize for posting on an old thread. My issue seems closely related. While webstorm finds and suggests the import correctly, it does not appear to be able to switch between the project language (ES6) and the dependency's language (CommonJS). Therefore it continues to say `Cannot resolve symbol` (and offer no code completion) despite the project running correctly.
>While webstorm finds and suggests the import correctly, it does not appear to be able to switch between the project language (ES6) and the dependency's language (CommonJS).
haven't got what you mean. Please share code snippets/screenshots that illustrate the issue
My project settings are ES6, but the module I'm importing is CommonJS style. My project runs, so the import works, but WS cannot find the reference. (it's in the node_modules directory)
Thanks, reproduced; please follow https://youtrack.jetbrains.com/issue/WEB-50306 for updates
Oblio it appears to be an issue with typings (d.ts files) provided by minecraft-protocol module: files that have top-level imports and "declare" modules are considered as augmentations, not like regular modules. See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation; that's why the IDE can't recognize the modules exported by this package when analyzing the d.ts files.
I'd suggest filing a ticket to https://github.com/PrismarineJS/node-minecraft-protocol/issues
Honestly, I have no idea what that means. And, (assuming I did understand) I could submit a ticket with the package maintainers, I feel it's up to the IDE to do a better job handling the references. The fact is, the import works, the code runs. I can visually see the export, I can find where those objects are. I would expect the IDE could also.