Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable code completion for node_modules in WebStorm

I'm new to WebStorm, and fairly new to Node development. I am working on an existing project, and want to have code completion for my node_modules. More specifically, I'm using Chai and WebStorm doesn't seem to find the .have member of my expect.to statement.

This is my code, simplified:

var expect = require('chai').expect;

import {Customer} from '../../app/model/Customer.js';

describe('...', function() {
    it('...', function() {
        var customer = new Customer();
        expect(customer).to.have.property('name');
    });
});

I get squiggly lines under the have call, and WebStorm tells me Unresolved variable have.

If I F12 on the to, WebStorm takes me to another node module, shelljs, but I haven't imported that one.

Is this because WebStorm can't resolve everything in javascript?

I've enabled Coding Assistance for NodeJS as per the docs, but that made no difference.

like image 906
Peter Avatar asked Sep 06 '16 10:09

Peter


People also ask

How do you autocomplete in WebStorm?

Invoke basic completion By default, WebStorm displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Space or choose Code | Code Completion | Basic from the main menu. To get more suggestions, press Ctrl+Space for the second time (or press Ctrl+Alt+Space ).

Is WebStorm better than Vscode?

WebStorm is generally more efficient at refactoring and testing JavaScript and JavaScript-based code (such as TypeScript). Refactoring optimizes code for efficiency, while unit testing ensures product quality. VS Code still provides these features — but they aren't tailored to JavaScript.

How do I update node in WebStorm?

Enable Live EditPress Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Debugger | Live Edit. Select Update Node. js application on changes.

How do I run code in WebStorm?

To run a script, open it in the editor or select it in the Project tool window, and then select Run <script file name> from the context menu. WebStorm creates a temporary run/debug configuration of the type Node. js. To run a test, click the gutter icon next to it or press Ctrl+Shift+F10 .


2 Answers

Problem is caused by weird dynamic way these chai methods are defined. As a workaround I can suggest using chai.d.ts:

  • Open "Settings | Languages & Frameworks | JavaScript | Libraries"

  • Click "Download..." button and select "TypeScript community stubs"

  • Find "chai" and click "Download and Install".

enter image description here

See http://blog.jetbrains.com/webstorm/2014/07/how-webstorm-works-completion-for-javascript-libraries/, 'Using TypeScript community stubs (TypeScript definition files)' for more information

like image 116
lena Avatar answered Sep 26 '22 21:09

lena


WebStorm 2020.1

TypeScript definitions can also be added directly via package.json:

  1. Open the project's package.json
  2. Position the cursor on the package (within the dependencies section)
  3. Press alt+enter (or click the light bulb)
  4. Choose Install '@types/name' (where name is the dependency)

For example:

WebStorm intention popup

like image 35
Nick Avatar answered Sep 24 '22 21:09

Nick