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
Related
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
I have deployed an app to heroku but it can't start because of the type error "express is not a constructor" in my index.js file.
Files can be found in https://github.com/Wachiye/blog.git
Express is just a factory function, not a constructor. So, you don't use new with. You would use it like this:
const express = require('express');
const app = express();
Shown right here in the doc.
In the future, please embed the relevant code portion into your question and format it appropriately. Stackoverflow has a whole bunch of reasons for wanting you to do it that way AND it will get you a faster answer. You get answers here faster when YOU make the question quick and easy to understand without going to external resources.
I am using angular 6.0, electron 2.0, typescript 2.9, nodejs 9.11 to make a desktop app using electron framework. I am struggling with accessing NodeJS native API from the typescript code. I have set "commonjs" in the "tsconfig.app.json" file. When I write : require('fs') or require('net') in any of the ts files which are part of angular application, the system isn't able to find those modules.
Only one solution has worked so far. It goes like this. First in 'native.js'
window.fs = require('fs')
Then in polyfill.ts :
declare global {
interface Window{
fs : any;
}
}
Then access fs in the rest of codebase as window.fs.
While this is okay, but it is not scalable as if I have to use any library which depends on NodeJS native API, then that library has to be imported through this mechanism.
Is there any other solution to let angular allow importing of nodejs system libraries through normal require(<module>) syntax?
import * as Fs from 'fs';
const fs: typeof Fs = window['require']('fs');
on my side, this work if you use typescript for ref.
You need give a defenition or is will any.
Using this for nwjs, know nodejs is global.
const fs: typeof import('fs') = require('fs');
I should use a plugin named hapi-mongoose-db-connector into my hapijs application. In the repository page the developers suggest the ways you can import correctly it. It says that the following way is the bad way:
# from the server
mongoose = server.pack.plugins['hapi-mongoose-db-connector'].mongoose
# or from a plugin
mongoose = plugin.plugins['hapi-mongoose-db-connector'].mongoose
and discourages using it. Instead he recommends to do in the following way:
You do nothing and just require mongoose in your plugins. As npm
requires are singletons (the code is loaded only once this works very
well)
but he doesn't show any examples. At this point I'm not pretty sure how to use it. I wouldn't call in every js files mongoose. I would call it once in my application somewhere and in my js files where I create models for the database, use it. Do you know any best practices in those cases?
Actually, first one is the hapi way doing this kind of thing.
But as the mongoose module is a singleton, that plugin just require mongoose and initialize it [1] after load that plugin into hapi, you can use mongoose in any file;
var mongoose = require("mongoose");
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.