Why am I getting a "Cannot find module" err when compiling Typescript? - node.js

I am just getting started using typescript on the server and I'm pretty much stuck trying to import 3rd party npm modules. Here is what I have declared:
import mongodb = require('mongodb');
import assert = require('assert');
import Q = require('q');
... and I am getting the following errors when compiling:
src/Databse.ts(1,26): error TS2307: Cannot find module 'mongodb'.
src/Databse.ts(2,25): error TS2307: Cannot find module 'assert'.
src/Databse.ts(3,21): error TS2307: Cannot find module 'q'.
What is the correct way to import 3rd party modules?

It doesn't feel the correct way to import 3rd party's library. You can simply use only import
import "library";
OR
import {module} from "library";
Note:- when you are using above syntax then you need to make your that
the module you are importing exist otherwise you will get the error
that it can't find the module you are trying to import.
If you want to use webpack then
require("library");
Or like this
var module = require("library");

Have you installed the typing declarations ?
typings install mongodb --save
typings install dt~assert --save
typings install dt~q --save
Then you will need to reference the typings.
Either using the triple slash-directive
/// <reference path="..." />
More about it here Triple-Slash Directives
Or add the typings index.d.ts file in files array in tsconfig.json
"files": [
"./typings/index.d.ts",
]

Related

mockingoose does not work with TypeScript

The following import
import mockingoose from 'mockingoose';
gives an error Could not find a declaration file for module 'mockingoose'. with TypeScript.
According to this npm page,
mockingoose provides its own type definitions, so you don't need #types/mockingoose installed!
I'm not sure how to fix this.

How to require a node package that wants to be a module, without type: module

I need to import a module to my project.
When I do const gifsicle = require('gifsicle'); it tells me:
ReferenceError: require is not defined in ES module scope, you can use import instead
When I try to do import, it says
SyntaxError: Cannot use import statement outside a module
Other threads say to fix this I have to add "type": "module" to my package.json
But doing so gives me this error:
ReferenceError: require is not defined in ES module scope, you can use import instead
How do I load in an ES module package into my current non-module package which uses require?
First of all add "type":"commonjs" to package.json.
Downgrade your package version. If you are using let suppose version 8.0.0 then install older version like npm i package-name#7.1.0.
That's all now enjoy coding...

Why does "require" compile but "import" does not?

I am new to TypeScript/JavaScript and Node.js and writing a simple script to run in command line.
I installed archiver dependency and added import archiver from 'archiver'; to my script.
When I compile the script with tsp -p . the import does not compile:
error TS7016: Could not find a declaration file for module 'archiver'. <my script path> implicitly has an 'any' type.
Try `npm install #types/archiver` if it exists or add a new declaration (.d.ts) file containing `declare module 'archiver';`
6 import archiver from 'archiver';
~~~~~~~~~~
However const archiver = require('archiver'); does compile. Now I wonder what's wrong with the import.
You installed the npm packages without the types, meaning the pure JS package. You would need to run npm install #types/archiver, provided this package supports Typescript. The standard for JavaScript (ES6/2015) suggests to use import and not the require.

Typescript Cannot find Module "fs" even though #types/node installed

I know the usual fix for this one is to install #types/node , and I have 10.12.23 of that installed.
This appears to be a strange error and I am a bit baffled by it. I have 2 other npm modules installed: config ( which requires #types/config ) and firebase-admin which must have it's own typescript types. Also using VS code version 1.31 . I even tried installing an older version of #types/node
The following works fine
import admin from "firebase-admin";
import fs from "fs";
The following fails: cannot find module 'fs'
import admin from "config";
import fs from "fs";
The following fails: cannot find module 'fs'
import fs from "fs";
I am not using any other packages / webpack or anything else. Any ideas are appreciated.
Besides installing #types/node, check that the tsconfig.json:
does not have a compilerOptions types, or make sure "node" is included there
noResolve does not exist or is set to false
esModuleInterop is set to true or you'll get error Module "fs" has no default export
Import fs in your ts/js file
import fs from 'fs'

TypeScript #types modules fail to resolve each other

When I use npm to install TypeScript definitions like this
npm install --save #types/express
I cannot use the installed modules as they fail to resolve each other. For example, #types/express requires #types/express-static-server-core, but as #types/express/index.d.ts contains a relative path to express-static-server-core, the module cannot be resolved:
node_modules/#types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
node_modules/#types/express/index.d.ts(17,23): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/#types/serve-static/index.d.ts(15,26): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/#types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.
How can I solve this issue? What is the best way to install TypeScript definitions?
As far as I know, typings is deprecated, so I tried to just install the type definitions from #types and then use
tsc --target ES5 --module commonjs index.ts
but it does not work yet. What am I doing wrong?
On the chance this is related, in one project, I was using generic types with express:
import { ParamsDictionary, Request, ... } from 'express-serve-static-core';
// ...
app.get<ParamsDictionary, UserDetails>(...);
That worked. When I tried to do the same thing in another project that wasn't using generic types:
import { Request, Response } from 'express-serve-static-core';
I was getting 'Unable to resolve path to module 'express-serve-static-core'.
When I changed my import to:
import express, { Request, Response } from 'express';
The problem went away.

Resources