Typescript project compile errors - node.js

I am starting with typescript and installed the typing files for node and express,body-parser. When i try to run compile , but on compiling I see the errors that it cannot import express and body parser. But I also installed the node modules separately for all of them so after tsc compiles the code it would run fine . But the compilation errors are still there.
The Errors
app.ts(2,26): error TS2307: Cannot find module 'express'.
app.ts(9,12): error TS2304: Cannot find name 'process'.
import express = require('express');
let app = express();
app.get('/',(req,res)=>{
res.send("Hello");
})
// Listen for HTTP traffic
app.listen(process.env.PORT || 3000);

code it would run fine
TypeScript is like a really powerful linter. It will always try to give you JavaScript even in the presence of type errors. So your code might run fine even with errors.
More
https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html
But the compilation errors are still there.
I cannot help you much without you sharing more code / steps you took?
Samples
This project uses express : https://github.com/alm-tools/alm
Also docs on a quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

So , first mistake with my code i found was that i my app.ts i was not referencing the main.d.ts file.
/// <reference path="typings/main.d.ts" />
then still the errors were coming .
so installed typings for the following.
typings install serve-static --ambient --save
typings install express-serve-static-core --ambient --save
typings install mime --ambient --save
and voila, no compile errors

Related

Nodejs: Cannot find module `nanoid`

When I try to compile my Typescript project (NestJS) I came across the following problem.
Some packages like nanoid, cookie-parser give error Error: Cannot find module 'nanoid'
The code where they used is below:
...
import { nanoid } from "nanoid";
...
const confirmToken = nanoid(32);
...
Here are what I tried:
I checked the node_modules directory. The package is there
Tried to completely remove node_modules and package.json.lock.
Used v16.14.0, v17 versions of NodeJS
Used yarn and npm
None of these ways helped.
Again, this error happens with only certain packages.
What could be an issue?

Ionic 2 cannot find module 'dgram'

I have installed a template Ionic 2 application and want to add the NPM package bonjour
After installing and including the package in my component like this:
var Bonjour = require('bonjour');
var bonjour = new Bonjour();
The application won't run stating 'cannot find module dgram'
The application has both the bonjour package and bonjour types installed.
The problem
The application can't find the module dgram which is located in the #types/node file. The project is running TS 2.4.2 and should not need any references to the #types, this should be picked up automatically.
What have I tried
I tried including the #types folder anyway in multiple ways, by setting typeroots or types in the ts.config.json file. This didn't change anything.
I tried specifying types :
"types": ["node", "bonjour"]
I tried reinstalling all node modules and clearing the cache
I tried including a reference path in my component above the require statement:
/// <reference path="node_modules/#types/node/index.d.ts" />
var Bonjour = require('bonjour');
var bonjour = new Bonjour();
This all did not help. Any ideas on how to make my application load this module properly?
The package Bonjour has a DatagramPlugin which require dgram to function properly. In Ionic 2 this package is not available. The solution is to use the Native Zeroconf package as an alternative.
dgram library is included with node.js since v0.1.99 as seen here.
You will always have dgram defined as long as you use a node version post v0.1.99. Your problem is only with Typescript types.
Make sure you are installing node types with npm i --save-dev #types/node and that you are including the es6 lib in your tsconfig.json file.
If the previous step does not work add this on the top: import * as dgram from "dgram";
If nothing works you can copy the module definition from here export it yourself.
Extra tip: If you do not trust your tsconfig.json for some reason pass the lib and types argument directly in the tsc command such as: tsc --lib es6 --types node -p .

error TS2307: Cannot find module 'bluebird'

I am currently trying to develop an app using Ionic 2 and Angular 2 with Typescript version. I decided to use the library amqp-ts to include messaging in my app. I installed the library via npm like:
npm install amqp-ts
Everything went fine and now I've got something like this:
/ app root directory
+ node_modules
- amqp-ts
- lib
- amqp-ts.d.ts
- node_modules
- amqplib
- bluebird
- winston
The problems begin now: I import the library into my component as it is done in the example of the documentation...
import * as Amqp from "amqp-ts";
... and when I try to deploy the app I get the next error messages:
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(2,26): Error TS2307: Cannot find module 'bluebird'.
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(50,12): Error TS2304: Cannot find name 'Buffer'.
1. The line related to the first error message
// exported Typescript type definition for AmqpSimple
import * as Promise from "bluebird";
[...]
2. The line related to the second error message (same file: amqp-ts.d.ts)
export class Message {
content: Buffer;
[...]
}
I hope you can help me, please.
Additionally to regular package install you need to install TypeScript typings. Typings are like header files, they contain all methods/classes/interfaces definitions.
To install typings you need a typings tool. The best way is to install it globally so you can use it in every project
npm install typings --global
Then installing new typings inside your project is pretty simple, first search for library:
typings search bluebird
Install it :
typings install --save bluebird
More info : https://github.com/typings/typings
I too faced same issue, but for me above answer is not working.
While simply running :
npm i bluebird
resolving the issue

browserify + gulp + react cannot find module react

I started to work on a new project and I need to set up the environment. All the others team members work with a OS X environment while I'm using Ubuntu 14. The project is already working for them but I'm having problems setting up.
I'm getting this error when I try to run
$ gulp watch
Cannot find module 'React' from '/home/camilatigre/dev/kahuso/kahuso-ux/scripts/components/company/dashboard'`
And if I try to run browserify I also get a similar error:
$ browserify -t reactify ./scripts/app.jsx > app.js
Error: Cannot find module 'React' from '/home/camilatigre/dev/kahuso/kahuso-ux/scripts/components/company/dashboard'
at /home/camilatigre/.nvm/versions/node/v0.12.7/lib/node_modules/browserify/node_modules/resolve/lib/async.js:46:17
at process (/home/camilatigre/.nvm/versions/node/v0.12.7/lib/node_modules/browserify/node_modules/resolve/lib/async.js:173:43)
at ondir (/home/camilatigre/.nvm/versions/node/v0.12.7/lib/node_modules/browserify/node_modules/resolve/lib/async.js:188:17)
at load (/home/camilatigre/.nvm/versions/node/v0.12.7/lib/node_modules/browserify/node_modules/resolve/lib/async.js:69:43)
at onex (/home/camilatigre/.nvm/versions/node/v0.12.7/lib/node_modules/browserify/node_modules/resolve/lib/async.js:92:31)
at /home/camilatigre/.nvm/versions/node/v0.12.7/lib/node_modules/browserify/node_modules/resolve/lib/async.js:22:47
at FSReqWrap.oncomplete (fs.js:95:15)
I already tried to reinstall npm, nodejs, browserify, reactify, clean the node_modules folder and re-add all the packages but nothing works.
Any suggestions? :)
i found a solution. 2 archives were using a require('React') when it should be a lowercase r.

Visual studio 2015 - node.js, express 4 using typescript project does not compile

There does not exist a project template that uses Typescript with Express4, there does however exist typescript template using express3. Is it that TS is incompatible with Express4.
I tried by creating regular TS with Express3 template, uninstalled Express3 npm package and added Express4 package. It did not compile.
I also added TDS as explained in https://code.visualstudio.com/Docs/runtimes/nodejs and it gave more errors like cannot find property express.favicon().
Simply create "Basic Node.js Express 4 Application" JavaScipt type application and after remove app.js and add app.ts. After it restart VS and now you should see TS as project icon.
Also run this in package manager console
npm install tsd -g
tsd install express --save
and add this line in the top of app.ts
/// <reference path="path to your tsd.d.ts" />
usually tsd.d.ts will be placed in the root of your solution folder in typings folder

Resources