Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Cannot find module 'http'. (2307)" with "@types/node" under typescript@2.0.3 #216

Closed
huan opened this issue Oct 11, 2016 · 15 comments
Closed

Comments

@huan
Copy link

huan commented Oct 11, 2016

TypeScript Version:

2.0.3

Code

// A *self-contained* demonstration of the problem follows...
// t.ts
import * as http from 'http'
console.log(typeof http)
$ ./node_modules/.bin/ts-node -v
ts-node v1.4.1
node v6.0.0

$ ./node_modules/.bin/tsc -v     
Version 2.0.3

$ npm install @types/node
wechaty@0.4.0 /Users/zixia/git/wechaty
└── @types/node@6.0.45

$ ./node_modules/.bin/ts-node --no-cache t.ts

D:\cygwin64\home\zixia\git\wechaty\node_modules\ts-node\dist\index.js:166
                    throw new TSError(diagnosticList);
                          ^
TSError: ⨯ Unable to compile TypeScript
t.ts (1,23): Cannot find module 'http'. (2307)

Expected behavior:

should import http as well

Actual behavior:

TS2307 error

Another STRANGE behavior

if I add another @types into t.ts, it will work without any problem.

$ cat t.ts
import * as http from 'http'
console.log(typeof http)

import * as express from 'express'
console.log(typeof express)

zixia@zixia-desktop ~/git/wechaty
$ ./node_modules/.bin/ts-node --no-cache t.ts
object
function

very interesting, what's wrong with me?

@huan huan changed the title "Cannot find module 'http'. (2307)" with "@types/node" under typescript@next "Cannot find module 'http'. (2307)" with "@types/node" under typescript@2.0.3 Oct 11, 2016
@ljani
Copy link
Contributor

ljani commented Oct 14, 2016

I have the same problem. It seems I can workaround it using the following directive at the top of my file (which is located in ./src/):

///<reference path="../node_modules/@types/node/index.d.ts"/>

Running tsc works fine. ts-node seems to be missing the global references tsc has.

EDIT: Here's another workaround, which seems a little better: #179 (comment)

{
    "compilerOptions": {
        "types": [
          "node"
        ]
    }
}

@paulsouche
Copy link

Hi,

I had the same issue with mocha on #246

The problem is that module http is ambient declared by node declaration file. What ts-node do is

  • find http directory in @types folder
  • no directory - Error

My first solution was to import module mocha

import * as mocha from 'mocha';

To have all mocha declarations available but I did not know that we could declare an ambient module via

/// <reference types="mocha" />

Declaring mocha as ambient in tsconfig.json did not work for me

{
  "compilerOptions": {
    "types": ["mocha"]
  }
}

I don't know why exactly because it should be equivalent...

Again I faced a new issue not declaring these references outside of my tests in a separated file with only one spec file.

It works with two spec files ??? I think this is most a mocha --compilers issue.

I've created a repo here explaining the solution.

@MrCrimp
Copy link

MrCrimp commented Dec 12, 2016

My fix for windows, after trying most suggestions, was to remove types and instead use

"typeRoots": [
      "node_modules/@types"
    ],

in combination with adding this to my src/typings.d.ts

/// <reference types="@types/mocha" />

Tsnode 1.7.0
TypeScript 2.1.1 and 2.1.4

budiadiono referenced this issue in vue-typed/vue-typed Jan 11, 2017
MartinSGill added a commit to MartinSGill/discovery-writer that referenced this issue Feb 3, 2017
Fixes the "npm run test" commad error, based on a comment from: TypeStrong/ts-node#216 (comment)
@samvloeberghs
Copy link

@MrCrimp although that also solved it for me, the bug remains.

As per the documentation of TypeScript:
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.

Be aware that using this solutions, you may introduce some side effects / unexpected behavior:

If typesRoots is specified, only packages under typeRoots will be included.

MartinSGill added a commit to MartinSGill/cspell that referenced this issue Feb 23, 2017
Fixes Windows builds using ts-node module, to resolve the following issue: TypeStrong/ts-node#216
@renehamburger
Copy link

A combination of 'typeRoots' and types worked for me on Windows (TypeScript@2.2.1, ts-node@2.1.0):

"compilerOptions": {
  "typeRoots": [
    "node_modules/@types"
  ],
  "types": [
    "mocha"
  ]
}

(On OS X, neither of these was needed, and I'm sure the same is true for Linux.)

@massimocode
Copy link

We are facing what seems to be a related issue. In our scenario, we have multiple typeRoots and packages are not being found in those typeRoots. The code can be seen here:
SMH110/Pizza-website#198

And the error can be seen here:
https://app.shippable.com/runs/58bd1167ba295305001507e7/1/console

However, what we're seeing is that it works fine on Windows, and we only get this error on Linux.

@blakeembrey
Copy link
Member

This should be resolved with v2.1.1 now, let me know how that goes!

@kalahari
Copy link

kalahari commented Jun 29, 2017

I am getting this with ts-node 2.1.2 and typescript 2.4.1 (on node 6.10.3 on macOS). If I take typescript back to 2.3.4 it works.

@collinforrester
Copy link

I am also getting this on ts-node 3.0.4 and typescript 2.4.1 (node 6.10.0 on mac). If I downgrade typescript to 2.3.4 it also starts working for me again.

@jochenseeber
Copy link

jochenseeber commented Jul 4, 2017

I also have this problem on MacOS. I created a minimal test project. Running

yarn install
gulp

results in the following output:

[00:37:40] Requiring external module ts-node/register

/Users/jochen/Projects/Scratch/node-test/node_modules/ts-node/src/index.ts:296
        throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
              ^
TSError: ⨯ Unable to compile TypeScript
gulpfile.ts (3,29): Cannot find module 'gulp-typescript'. (2307)
    at getOutput (/Users/jochen/Projects/Scratch/node-test/node_modules/ts-node/src/index.ts:296:15)
    at /Users/jochen/Projects/Scratch/node-test/node_modules/ts-node/src/index.ts:325:16
    at Object.compile (/Users/jochen/Projects/Scratch/node-test/node_modules/ts-node/src/index.ts:479:11)
    at Module.m._compile (/Users/jochen/Projects/Scratch/node-test/node_modules/ts-node/src/index.ts:379:43)
    at Module._extensions..js (module.js:580:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/jochen/Projects/Scratch/node-test/node_modules/ts-node/src/index.ts:382:12)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)

@blakeembrey
Copy link
Member

blakeembrey commented Jul 5, 2017

@jochenseeber Your issue is caused by a lack of moduleResolution in your tsconfig.json file and/or having it set to module: es6. If you were emitting commonjs, it would default to node module resolution. I'm not sure that's a regression since non-CommonJS code would never run on node.js anyway, but if someone wants to create a demo of something that has regressed please share.

@nevir
Copy link

nevir commented Jul 8, 2017

I'm also seeing this on OS X with ts-node@3.2.0, typescript@2.4.1 and the config below.

tsc compiles without complaint, but ts-node fails to find any types declared via @types/* packages

{
  "compilerOptions": {
    "charset": "utf-8",
    "declaration": true,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "jsx": "react",
    "lib": [
      "dom",
      "es2016",
      "es2017.object"
    ],
    "module": "commonjs",
    "newLine": "LF",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "./dist/",
    "pretty": true,
    "rootDir": ".",
    "sourceMap": true,
    "strictNullChecks": true,
    "target": "es5"
  }
}

@danielpza
Copy link

danielpza commented Jan 29, 2018

I have the same problem on linux,

$ tsc --version
Version 2.6.2
$ node --version
v6.12.3
$ cat tsconfig.json 
{
    "compilerOptions": {
        "rootDir": ".",
        "module": "commonjs",
        "target": "es6",
        "strict": true,
        "noImplicitAny": false,
        "noImplicitThis": true,
        "moduleResolution": "node",
        "outDir": "./dist/",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*"
            ]
        }
    },
    "include": [
        "./index.ts"
    ]
}
$ cat index.ts 
import * as http from "http";
$ tsc -p .
index.ts(1,23): error TS2307: Cannot find module 'http'.

@paulsouche
Copy link

paulsouche commented Jan 30, 2018

@danielpa9708 add

{
  "typeRoots": [
    "./node_modules/@types"
  ],
  "types": ["node"]
}

In your compilerOptions and be sure to have @types/node installed

@NoelSkenshee
Copy link

NoelSkenshee commented Jun 9, 2022

The solution to this problem is simple than you think :
1 npm i -D typscript
2 npx tsc init
3 npm i ts-node
4 in youre package.js>script>start="ts-node main-file.ts"
5 youre terminal > npm start

💯 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests