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
[email protected] /Users/zixia/git/wechaty
└── @types/[email protected]
$ ./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?
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: https://github.com/TypeStrong/ts-node/issues/179#issuecomment-249025729
{
"compilerOptions": {
"types": [
"node"
]
}
}
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
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.
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
@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.
A combination of 'typeRoots' and types worked for me on Windows ([email protected], [email protected]):
"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.)
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:
https://github.com/SMH110/Pizza-website/pull/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.
This should be resolved with v2.1.1 now, let me know how that goes!
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.
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.
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)
@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.
I'm also seeing this on OS X with [email protected], [email protected] 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"
}
}
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'.
@danielpa9708 add
{
"typeRoots": [
"./node_modules/@types"
],
"types": ["node"]
}
In your compilerOptions and be sure to have @types/node installed
Most helpful comment
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/):Running
tscworks fine.ts-nodeseems to be missing the global referencestschas.EDIT: Here's another workaround, which seems a little better: https://github.com/TypeStrong/ts-node/issues/179#issuecomment-249025729