WebStorm code completion for Node shows too many options - node.js

I've created a small project with NodeJS and TypeScript code. Installed the type definition files there (tsd install node). The code starts with these lines:
var http = require('http');
var server = http.createServer(...)
When I open this code in WebStorm 11 it shows me hundreds of options in a context-sensitive help window when I hit CTRL-Space after http.
I tried adding /// <reference path="typings/node/node.d.ts" /> as the first line, downloaded and installed DefinitelyTyped community stub, but it still shows tons of options for the http object.
When I open the same file in Visual Studio Code, it shows me a short list of API related to Node's http module. How to teach WebStorm to be smarter with code completion?

I tried adding /// as the first line, downloaded and installed DefinitelyTyped community stub, but it still shows tons of options for the http object.
This is because you are using var/require. This means that webstorm is being heuristic in its suggestions. You should use import/require to narrow it down to just what is actually declared for the http module:
import http = require('http');
var server = http.createServer(...)
More on import : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Related

Remove warning on Node.js WebStorm

I am learning backend with Node.js and using middleware to catch for errors and send a different response code and body.
The warning is similar to err.message.
How do I remove these warnings?
Are you using express? Its methods are generated dynamically in runtime, so they can't be resolved during static code analysis. Installing TypeScript stubs should help to get methods work: put cursor on 'express' in const express = require('express'); , hit Alt+Enter and choose Install TypeScript definitions for better type information to install typings - see https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html#ws_jsconfigure_libraries_ts_definition_files

Missing Node Methods?

Working with Node 12.16.1 I can't seem to call the createServer method from the http module in IntelliJ. Also, I'm a noob (2nd post on stackoverflow) so I'm sure there is something really basic I'm missing. The first step toward learning from my mistakes is identifying them so your help is appreciated.
I know createServer is no longer used in Express 4, but it still shows as being a current method for the http Node 12.16.1 documentation online:
https://nodejs.org/dist/latest-v12.x/docs/api/http.html
https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_http_createserver_options_requestlistener
So when I start a file with
var http = require('http');
I figure I should be able to see createServer as an available method when I enter:
http.
I know there are other ways to create a server using express 4 and coding a function, so I'm not trying to figure out how to enable a server. I want to better understand the behavior of modules and methods and installing them. I have run into "missing methods" from node a couple of times and have usually found a work around.
This time I want to better understand the reason I'm not finding the method in my IDE. Is it deprecated? Is there something I need to configure differently? I have tried
npm install http
a couple of times and still don't find the method. The module call is recognized and numerous methods are available. Again I'm a noob and just looking to learn / understand this behavior. Any help is appreciated.
Thanks
With require it won't popup with intellisense, only if you use ES6 import syntax like this. So basically it will work with require too, but with import you can use only those parts which are needed for you. Take a look at here: The difference between "require(x)" and "import x"
import http, { createServer } from 'http';
http.createServer()

Error importing superagent in React code

I have a NodeJS server code written in JSX where I'm importing superagent like so:
import request from 'superagent';
When server starts after build, I get the following error:
TypeError: require is not a function. And this happens at the following line in my compiled code:
var crypto = require('crypto');
On tracing back I realized that crypto is required by 'formidable', which is required by 'superagent'.
I have the same superagent import in my client side javascript code but that works fine. I diffed the compile JS code for node(server), and client, and both are the same. So it doesn't seem like an issue with the way its build.
Has anyone seen this error before and would you know what needs to be done?
Found a solution to this here:
https://github.com/visionmedia/superagent/wiki/Superagent-for-Webpack
Adding the said plugin to web pack solved the issue.

Browserify the node.js http server

We created a simple js file, intending to find out if http.createServer works on client browser or not:
var http = require("http")
var server = http.createServer()
server.listen(9024, function () {
console.log("demo server listening on port 9024")
})
and embedded it into a html after browserify.
Display the html in chrome, unfortunately, it always fails on line 2 on http.createServer():
"Uncaught Type Error: undefined is not a function"
We also played around with "serve-browserify" a bit without success.
We have attempted the same thing on both chrome and firefox, and on Linux and Windows. All failed.
Searching through the web, there are quite a few examples for browserify http into the browser.
They all appear to be simple invocation of browserify. However we don't seem to be able to get the same good result.
Your help will be greately appreciated.
You can't use Node.js modules in the browser. All Browserify does is bundling CommonJS modules, it does not allow you to run server side code in the browser.

Debugging nodes src/http.js with node-inspector

I'm having some bad luck debugging a node.js application. The problem is that I need to set a breakpoint in a file that isn't loaded at start (when --debug-blk) hits in. The file is http.js from node itself.
So I would like to modify the file to add a debugger; statement, but I'm unable to find this http.js file on my disk? The only place I see it on my disk is in the source distribution of node (lib/http.js). Where is this file located after a make install in node-src, and how can I set breakpoints inside it?
http.js is internal to the node executable. It doesn't exist on disk, and the only way to modify it is to build node from source.
Do this:
var http = require('http');
debugger;
// the rest of your app
Your app will start, load http, and then pause. Now you can use the inspector to set breakpoints inside http. (You won't need --debug-brk since the debugger statement will pause the app before anything really happens.)

Resources