简体   繁体   English

为什么添加此内容需要导入才能将Node.js停止输出到控制台

[英]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: 我有app.ts:

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

console.log( "Hello Node" );

var checker: SampleClass = new SampleClass();

and sampleModule.ts: 和sampleModule.ts:

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

export = SampleClass;

(I also tried the below which I presume is ES5 module syntax) (我还尝试了以下我认为是ES5模块语法的内容)

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

and my tsconfig: 和我的tsconfig:

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

my app.js is completely empty. 我的app.js完全是空的。 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. 如果我删除导入(以及类的实例化),则console.log会出现在app.js中,但是导入后它完全为空。

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. 原来,问题与import语句无关,而与tsconfig.json中的out =“ app.js”无关。

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. 编译模块时--out(或实际上--outFile)不是有效的选项,因为这应该将整个应用程序编译为一个文件,这不是我们在使用外部模块时所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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