Remove warning on Node.js WebStorm - node.js

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

Related

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.

WebStorm code completion for Node shows too many options

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

Express 4.x with Typescript

I am trying to make a express app with typescript.
This is my code so far:
//<reference path="./server/types/node.d.ts"/>
//<reference path="./server/types/express.d.ts"/>
import express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('hi');
});
app.listen(3000);
nothing really shocking, I am just trying to make this work, but somehow, always when I try translate this file to a js file. I get strange errors, even if I change the express version to 3.1 (the express.d.ts is just supported for express 3.1 not for 4.x)
Any idea, where I can get a express.d.ts file for express 4.x or what I am doing wrong?
>> error TS2071: Unable to resolve external module ''express''
>> error TS2071: Module cannot be aliased to a non-module type.
>> error TS2095: Could not find symbol 'express'.
You reference comments are wrong. There needs to be three slashes /// :
///<reference path="./server/types/node.d.ts"/>
///<reference path="./server/types/express.d.ts"/>
The only way you can get that error if you are using this reference file https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts#L26 was that your reference comments were wrong and typescript was not reading that express.d.ts :)
Might I add that because you are using express 4.x and the type definitions are not up-to-date, you will be missing some key features that are central to express 4.x, such as Routers.
You have two choices at this point: update the type definitions, or use
var express = require('express');
instead, which removes some of the benefits that typescript provides, but is something you're bound to run into in the future with other node modules, such as mongoose.

Error with middleware and NodeJS

I have this :
app.use(session({secret: 'JKSBDFJKQS444SQ4DQSND'}));
But when I run my app file I got this error :
Error: Most middleware is no longer bundled with Express and must be installed separately.
How to fix ?
From the express 4.x Docs "As of 4.x, Express no longer depends on Connect. All of Express' previously included middleware are now in separate repos. Please view the list of middleware. The only included middleware is now express.static()." See here for the details.

Using Express() with TypeScript

I want to use the most recent version of Expess with node.js in TypeScript. The express.d.ts provided by microsoft in the samples seems to be built upon a versions prior to 3.0.x.
In previous version you could do
var app = express.createServer()
but after 3.0.x you should do:
var app = express();
Express.d.ts does not support this... I've found a hack around this:
I've added the following line to Express.d.ts:
export function(): any;
In app.ts when I want to create the app object I do the following:
var app = <express.ExpressServer>express();
This seems to fix the issue, it's compiling without an error, and also I get intellisense support. However this is a hack... First of all why can't I write something like this?
export function(): ExpressServer;
Is this the recommended way to fix this issue?
Pretty old discussion, but I ran into the same problem recently and found that there is a new express.d.ts that properly supports express 3 on the DefinitelyTyped site.
You should be able to add this ambient function declaration to express.d.ts to get what you want.
declare function express(): ExpressServer;
if you declare express this way: import * as express from "express", you will get this error in runtime, declaring it this way: const express = require "express", won't throw any error.
Also, don't forget to declare app variable or property type as express.Application
Here's a sample project - Express 4.x app in TypeScript: https://github.com/czechboy0/Express-4x-Typescript-Sample

Resources