简体   繁体   中英

Why does adding this require import stop Node.js outputing to the console

I am just starting on a new node project and still don't quite get how to work with external modules.

I have app.ts:

import SampleClass = require( "./sampleModule" );

console.log( "Hello Node" );

var checker: SampleClass = new SampleClass();

and sampleModule.ts:

class SampleClass {
    constructor() {
        console.log( "Hello from sample");
    }
}

export = SampleClass;

(I also tried the below which I presume is ES5 module syntax)

export class SampleClass {
    constructor() {
        console.log( "Hello from sample");
    }
}

and my tsconfig:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "out": "app.js",
        "diagnostics": true
    },
    "files": [
        "src/app.ts"
    ]
}

my app.js is completely empty. If I remove the import (and instantiation of the class) then the console.log appears in the app.js but with the import it is completely empty.

What am I doing wrong?

This has been driving me nuts! Such a simple thing but it WOULDN'T WORK!

Turns out that the problem wasn't anythign to do with the import statement but the out="app.js" in the tsconfig.json.

When compiling modules --out (or actually --outFile) is not a valid option as this is supposed to compile the whole app to one file which is not what we are doing when using external modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM