Typescript: "Cannot find module" with valid typings

typescript tutorial
typescript w3schools
typescript example
typescript download
typescript documentation
typescript vs javascript
learn typescript
typescript github

I just started a new nodejs project using typescript. I installed Typings (https://github.com/typings/typings) and used that to install reference files for node v4.x and express v4.x.

My node version is v4.2.6 My typescript version is v1.7.5

My project directory is laid out thus:

package.json
tsconfig.json
typings.json
src/
  app.ts
typings/
  main.d.ts
  main/ambient/node/node.d.ts
  main/ambient/express/express.d.ts

The contents of typings/main.d.ts are as follows:

/// <reference path="main/ambient/express/express.d.ts" />
/// <reference path="main/ambient/node/node.d.ts" />

The contents of tsconfig.json are as follows:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  }
}

The contents off typings.json are as follows:

{
  "dependencies": {},
  "devDependencies": {},
  "ambientDependencies": {
    "express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#dd4626a4e23ce8d6d175e0fe8244a99771c8c3f2",
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81"
  }
}

The contents of src/app.ts are as follows:

'use strict';

///<reference path="../typings/main.d.ts"/>

import * as express from "express";

This is exceedingly simple and should result in a basic app. However, when I try to compile this I get the error error TS2307: Cannot find module 'express'.

I have tried rearranging typings files, changing the relative path in the reference path tag, using the files field in tsconfig.json to indicate the reference paths instead of using an inline tag in the file, all to no avail. I have also tried compiling using gulp-typescript, gulp-tsc, and tsc directly on the command line.

I get similar errors when I try to use nodejs build-in modules such as crypto, http, fs etc.

These references seem valid -- what am I missing?

Triple-slash directives need to precede any statements in your file. Your "use strict" prologue needs to come after your reference comments as so:

///<reference path="../typings/main.d.ts"/>

'use strict';

import * as express from "express";

As a follow up to your comment where you're not getting any emit for your import: that's because TypeScript performs import elision. Unless you use the module for some value, the compiler won't emit the import because it assumes you only need that module for its types.

TypeScript, TypeScript adds optional types to JavaScript that support tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript� TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is pure object oriented with classes, interfaces and statically typed like C# or Java. The popular JavaScript framework Angular 2.0 is written in TypeScript.

microsoft/TypeScript: TypeScript is a superset of JavaScript , Today we're announcing our release candidate of TypeScript 4.0. While this RC brings us closer to a new major version of TypeScript, don't fret - there are no� TypeScript is a typed superset of JavaScript compiled to JavaScript. In other words, TypeScript is JavaScript plus some additional features. Features of TypeScript. TypeScript is just JavaScript. TypeScript starts with JavaScript and ends with JavaScript. Typescript adopts the basic building blocks of your program from JavaScript.

I know this is an old question, and sorry for bumping it but it comes up as one of the first links in Google.

Since the above didn't fix my issue and I eventually resolved it, I thought I'd share... In my case, it was relative pathing that caused the issue.

My typings were in

typings/bootstrap, typings/react-dom, typings/react, etc.

The react-dom one has an import from 'react'. To fix my code I had to edit it to be '../react/react'. Simple!

typescript, TypeScript is a strongly typed, object oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft. TypeScript is both a � TypeScript tutorial in Visual Studio Code. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust components.

None of the above worked for me - so I hope this will help someone else. (I'm using VS Code).

This issue suddenly happened to me out of the blue with one of my services, which was referenced in several modules. After saving a completely non-related component that DIDN'T use the service, two of the components using this service reported the 'cannot find module' error. I was able to fix it by simply changing the declaration to an invalid folder, saving the file and then changing it back to the correct path.

TypeScript, TypeScript 4.0 changes this which gives a great speed boost in these scenarios, and in turn improves --build mode scenarios (which imply both --incremental and --noEmitOnError). For details, read up more on the pull request.--incremental with --noEmit. TypeScript 4.0 allows us to use the --noEmit flag when while still leveraging --incremental

TypeScript - Overview, In TypeScript 4.0, users can customize the fragment factory through the new jsxFragmentFactory option. As an example, the following tsconfig.json file tells TypeScript to transform JSX in a way compatible with React, but switches each invocation to h instead of React.createElement, and uses Fragment instead of React.Fragment.

In TypeScript (and JavaScript), you can compare with either equality operator ('==') or strict equality operator ('==='). Both seems almost similar; but the way they compare two values is very different. Equals Operator ( == ) The comparison x == y with equals operator, where x and y are values, produces true or false. The […]

Find Find javascript developer on Answersite.com for Walnut. Check out results for Find javascript developer in Walnut

Comments
  • That is all valid. Share the project and I will have a look for you
  • Sweet thanks. github.com/jereynolds/ts-test
  • I was just poking around some more and I noticed that the reference path comment still exists in the .js output file. The import statement did get correctly resolved into a standard var express = require('express'); line
  • When I move the triple-slash above "use strict"; (or remove the "use strict"; line altogether) I just get an additional error in my compile step that the main.d.ts file cannot be found, even though the absolute path given in the error correctly points to the file
  • Do we need a reference path for each typings we are using? For me I had to find http-server what if I want to use FS as well?
  • If your tsconfig.json refers to the files, or you reference a file like main.d.ts that already lists them, then you shouldn't need to reference those files.
  • "Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." —how-to-answer
  • I installed typings for serve-static and mime, added the typings and node_modules folder to the exclude list in tsconfig.json, and moved "use strict"; to be after the reference tag. Now compilation succeeds with tsc -p ., but still fails when I try to use gulp to build. Any thoughts on why that isn't working?
  • @CSnover I did. The two bullet points are "most relevant parts of an important link" :)
  • @aaaarrgh I don't use gulp :)