error TS2307: Cannot find module 'bluebird' - node.js

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

Related

SwiperJS with ionic angular - Cannot find module 'swiper/angular' or its corresponding type declarations

I am trying to setup swiper.js in my ionic angular project. (Using the [ionic documentation])(https://ionicframework.com/docs/angular/slides)
When trying to import the module I get the error 'Cannot find module 'swiper/angular' or its corresponding type declarations.
When checking the node package there is no angular folder in the swiper package.
Swiper: 9.0.1
Node: v14.19.1
Angular 14
ionic: 6
Using the ionic documentation
npm i --save swiper
in app.module.ts I get the error when trying to import the swiper module:
import { SwiperModule } from 'swiper/angular';
Screenshot of code line
The contents of the swiper node package:
Screenshot of swiper folder contents
Swiper Version 9 doesn't have support this. They stopped supporting angular in an 'angular' way.
The recommendation is to use Swiper elements, which requires some re-learning of swiper.
Follow the links through this page to get set up.
https://swiperjs.com/angular

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?

log4js not recognized by intelliSense of VisualStudio 2017

My NodeJS project is written in Typescript 2.5
The following import line for the module:
import log4js= require('log4js);
compiles and i use the logger successfully. Even IntelliSense prompts the object method and properties well. Yet, i got following the annoying error from IntelliSense:
TS2307 (TS) Cannot find module 'log4js'.
If i try to add the respective types to my project but i got the message:
npm install #types/log4js --only=dev
npm WARN deprecated #types/log4js#2.3.5: This is a stub types definition for log4js (https://github.com/nomiddlename/log4js-node). log4js provides its own type definitions, so you don't need #types/log4js installed!

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 .

Typescript project compile errors

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

Resources