2

I need to use a simple node_module inside a simple typescript file, but it seems that the compiler doesn't want to get it.

Here's my simple ts file :

import glob = require('glob');
console.log(glob);

And I've got this error :

[13:51:11] Compiling TypeScript files using tsc version 1.5.0
[13:51:12] [tsc] > F:/SkeletonProject/boot/ts/Boot.ts(4,23): error TS2307: Cannot find external module 'glob'.
[13:51:12] Failed to compile TypeScript: Error: tsc command has exited with code:2

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Failed to compile: tsc command has exited with code:2

npm ERR! [email protected] start: `node compile && node ./boot/js/Boot.js`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] start script.

However, when I use a simple declaration in this same script, it works :

var x = 0;
console.log(x); // prints  0 after typescript compilation

What am I doing wrong in this case ?

EDIT:

Here's my gulp file :

var gulp = require('gulp');
var typescript = require('gulp-tsc');


gulp.task('compileApp', ['compileBoot'], function () {
    return gulp.src(['app/src/**/*.ts'])
        .pipe(typescript())
        .pipe(gulp.dest('app/dist/'))
});

gulp.task('compileBoot', function () {
    return gulp.src(['boot/ts/*.ts'])
        .pipe(typescript({
            module:'commonjs'
        }))
        .pipe(gulp.dest('boot/js/'))
});

gulp.start('compileApp');

Thanks for advance

Thanks for advance

6
  • Pretty sure you should simply write import glob or import 'glob' Commented May 15, 2015 at 12:00
  • import glob; throw me an error in IDE, and how should I refer to my library when using import 'glob' ? Thanks for your answer Commented May 15, 2015 at 12:02
  • It should be var glob = require('glob'); Y do you use import also? Commented May 15, 2015 at 12:19
  • Isn't this the standard typescript metod with import ? Commented May 15, 2015 at 12:19
  • Do you have correct --module option specified for the typescript compiler? For node it should be --module commonjs Commented May 15, 2015 at 12:56

2 Answers 2

2

You are using the correct syntax:

import glob = require('glob');

But the error: Cannot find external module 'glob' is pointing out that you are using a special case.

By default, the compiler is looking for glob.ts, but in your case you are using a node module, not a module that you have written. For this reason, the glob module will need special treatment...

If glob is a plain JavaScript module, you can add a file named glob.d.ts with type information that described the module.

glob.d.ts

declare module "glob" {
    export class Example {
        doIt(): string;
    }
}

app.ts

import glob = require('glob');

var x = new glob.Example();

Some Node modules already include the .d.ts in the package, in other cases you can grab it from Definitely Typed.

Sign up to request clarification or add additional context in comments.

2 Comments

It's really hard to manage and declare every modules with TypeScript so :-o . Is the Definitely Typed source really up to date and sure ? Thanks for your reply
Definitely Typed is community maintained and is currently receiving updates daily: github.com/borisyankov/DefinitelyTyped/commits/master
-3

Here is the error with your code

    import glob = require('glob');

Because in node.js import is not a reserved keyword. If you require any module in your application, you simply require it using the statement

    var glob = require('glob');

Once done you can then use

    console.log(glob);

To print the value of glob.Replacing import will hopefully do the job for you.

2 Comments

I m in a typescript file
please take the reference of this stackoverflow question in that case stackoverflow.com/questions/12958479/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.