1

I'm setting up a new node/typescript project, and having trouble getting the basics working.

'use strict';
let fetch = require("node-fetch");

Is giving me error TS2304: Cannot find name 'require'.

So I think typings are needed? Tried:

typings install node
typings install nodejs
typings install require

All of which give

Unable to find "XXX" ("npm") in the registry

What is the correct way to get require, or any simple node app working with typescript.

TS is great once you get going, but the shifting sands around initial configuration are quite a hassle to get going. I also looked at this answer, but it doesn't solve this issue. Some of the suggestions there no longer seem to work.

I'm trying to compile down to ES6. typings version 1.0.5

Using a tsconfig.json and a typings.json I have copied over from a separate working project and removed as much as possible. I guess I have to go through with tweezers and try to figure out what's different.

Community
  • 1
  • 1
dcsan
  • 11,333
  • 15
  • 77
  • 118

2 Answers2

3

TypeScript 2+ supports:

npm install --save @types/node

Typings:

typings install dt~node --global

and also check out the documentation on the Typings repo.

In general, you can try, eg: typings search node

and you should see the available types and their source.

eg: node has sources under dt and env.

dt is short for the Definitely Typed repository. This is a large GitHub repository of community made type definitions, but now you have to specify if you want to install it from here. Normally, the best source is from npm because these are hopefully maintained by the core team of that repository.

You can also specify versions [<source>~]<pkg>[@<version>][#<tag>] where <version> A semver range (E.g. ">=5.0")

Use --global flag for ambient types.

Global is the new ambient:

Usages of ambient are now global

That means in typings.json any ambientDependencies should be renamed globalDependencies and any ambientDevDependencies should be renamed globalDevDependencies.

It also means --ambient is now --global

For example, my typings.json had the following:

globalDependencies": {
  "node": "registry:dt/node#6.0.0+20160523035754",
Community
  • 1
  • 1
  • I assume that requireJS is from the project of that name, eg http://www.requirejs.org/ and not the built in node `require` command. I think there are instead some separate typings to just be able to use node's syntax extensions to javascript. without these, typescript cannot be used for node development. I did have this working in a previous project, but now it's not installable... – dcsan Jun 12 '16 at 10:53
  • there are some `node` modules listed as env but trying to install from here gives a very cryptic message: `typings install env~node *[master] typings ERR! message Attempted to compile "node" as an external module, but it looks like a global module.` as if its trying to compile node? – dcsan Jun 12 '16 at 10:55
  • For node I use the version from the `dt` repo. – RationalDev likes GoFundMonica Jun 12 '16 at 11:03
  • I added an update at the end with what is in my `typings.json` for node – RationalDev likes GoFundMonica Jun 12 '16 at 11:07
  • thanks, i haven't seen that syntax before. is that just for node6 or is it semver for the typing? (I'm stuck on node5 for now) – dcsan Jun 13 '16 at 09:55
  • I am using this with node 5. You can also specify versions `[~][@][#]` where ` A semver range (E.g. ">=4.0")` – RationalDev likes GoFundMonica Jun 13 '16 at 10:22
  • I had ` "ambientDependencies": { "node": "registry:dt/node#4.0.0+20160412142033" },` maybe global is the new ambient? – dcsan Jun 13 '16 at 10:39
  • Hmm, using your typings.json as above, i get this error on `typings install` `typings WARN deprecated 6/13/2016: "registry:dt/node#6.0.0+20160523035754" is deprecated (updated, replaced or removed)` – dcsan Jun 19 '16 at 21:31
  • this does seem to work though. I will memorize this incantation for next time I need to do node stuff with typescript. Although by then it will probably be not working. `typings install dt~node --global` – dcsan Jun 19 '16 at 21:35
  • That's good. Isn't that the same as the quote from Basarat's book? I'll move it up the top of the question, so you might like to mark it resolved. Yes, you are probably correct about it changing next time. Microsoft recently announced their plans for TypeScript 2.0 https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/ – RationalDev likes GoFundMonica Jun 20 '16 at 05:30
  • well your answer talks about requireJS which is a completely different thing from nodeJS' require, right? – dcsan Jun 20 '16 at 16:44
  • thanks for the answer, accepted. but man, what a convoluted mess, to just use node and typescript together. Like, what else do people use typescript for? and it's this ridiculously obscure to get basics working? anyone else: just use this: `typings install dt~node --global` – dcsan Jun 21 '16 at 14:47
  • Given it is developed by Microsoft and originated in the Visual Studio IDE, a few people are probably using it with .NET ;) But yes, is not the easiest tool to use, although it has been improving. – RationalDev likes GoFundMonica Jun 21 '16 at 15:06
  • Using typescript has become terribly messy and limiting. Think it's a little sad we have to read a book in order to understand something as simple as importing a module.. – Oliver Dixon Jul 23 '16 at 12:51
  • It's pretty sad people down vote for quoting a book and providing a valid answer. – RationalDev likes GoFundMonica Jul 23 '16 at 13:13
0

For those who just want to know how to get out of this mess, use this:

typings install dt~node --global
dcsan
  • 11,333
  • 15
  • 77
  • 118
  • For now this command will not install node typings (does nothing). If I have installed it before, `typings install` will emit this warning: `typings WARN deprecated 2016-06-22: "registry:dt/node#6.0.0+20160608110640" is deprecated (updated, replaced or removed)`. – hal Jul 01 '16 at 19:03