How to set up Node.js + Eclipse + TypeScript correctly?

Just started to get warm with Node.js + Eclipse + TypeScript and has been banging my head for days. Here is the code for my Server.ts file that I used on the Node.js website (just added the reference path at the top):

/// <reference path="node.d.ts" />

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

      

The problem is that if I don't use the link path at the bottom and use the Node.d.ts file then it shows "Unable to find name" where I include the http module. But if I use Node.d.ts and link to it, then it shows a bunch of errors inside Node.d.ts ():

import events = require("events"); // shows no such file or directory events.ts
import net = require("net"); // shows no such file or directory net.ts

      

Indeed, there are no such files, and even they should be js files, not ts (imho). Anyway, running "tsc -v" shows me that I am using TypeScript 1.3. After googling I found and used this node.d.ts file for my TypeScript 1.3 . However, it looks like this is the wrong file, or I am missing some point.

I am still confused a lot about these * .d.ts files. My eclipse project created a link to some lib.d.ts files, but it doesn't help if I try to link to it instead of the manual Node.d.ts file. Why do we need them for even fundamental commands like require? And of course any help would be appreciated :)

Update here a screenshot about Node.d.ts complains:

enter image description here

Update2 Finally I got it :) Looks like TypeScript itself doesn't know anything about the special javascript functions that are present in Node.js (these functions are not present in the regular javascript browser). This is why TypeScript compilers complain about the need to be Node.js specific stuff. So we need some bridge definition for the TypeScript compiler about Node.js specific things. Therefore, we need to enable Node.d.ts. We need to do the same for other javascript frameworks or libs to be able to use them in TypeScript. The question remains why is Eclipse complaining about my Node.d.ts file ...

Update3 . I have installed the Enide Studio 2014 plugin for node which includes the Typecs plugin on my work computer (Maverics). Here's my installed list of plugins for Eclipse:

enter image description here

Now I noticed that there is no Enide plugin and I remember that it took quite a long time to install it yesterday, so thanks to @SteveFenton's place this could be a problem. Started installing the Enide plugin again today and here is a screenshot of the installation process:

enter image description here

The same installation was successful in the same version of Eclipse (Luna) on my Yosemite home computer.

+3


source to share


2 answers


You can use import

, not var

:

/// <reference path="node.d.ts" />

//var http = require('http');
import http = require('http');

      



This will work even if http

it is not a file. Everything in node.d.ts

must be accessible in this way. If you are using other node stuff like express you will need additional files .d.ts

.

+2


source


Thanks to @SteveFenton I was able to spot my own silly mistake. It's just that I didn't work with Eclipse and because of this I didn't report that the Enida for node plugin was not fully installed. Here is the screenshot I have. It still needs to be investigated why it is not installed correctly.

enter image description here



Here are the errors:

enter image description here

+2


source







All Articles