未捕获的ReferenceError:require未定义TypeScript

时间:2017-10-29 06:32:42

标签: typescript npm

我正在尝试从一个.ts文件导出命名空间,并从其他.ts文件导入并收到错误NewMain.ts:2 Uncaught ReferenceError: require is not defined。我是新手,实际上是在学习TypeScript。这是我的tsconfig.json文件

{
 "compilerOptions": {
     "module": "commonjs",
     "noImplicitAny": true,
     "removeComments": true,
     "preserveConstEnums": true,
     "sourceMap": true
 },
 "files": [
     "greeter.ts",
     "Main.ts",
     "RajIsh.ts",
     "NewMain.ts"        
  ],
 "exclude": [
    "node_modules"
  ]
}

这是我导入名称空间的NewMain.ts

 import {DepartmentSection} from "./RajIsh"
 class Employee{
     name: string;
     //Function
     Display(username:string)
     {
         this.name=username;
         console.log(this.name);
     }
 }
 var person = new Employee();
 var itCell= new DepartmentSection.ITCell("Information Technology Section");
 console.log("Displaying from NewMain.ts by importing......");
 console.log(person.Display("XYZ")+" belongs to "+  itCell.DisplaySectionName("Finance Revenue and Expenditure Section"));
 console.log("Wooooooooo Hurrey!!!!!!!!!!!!!!!......");

这是RajIsh.ts

中的命名空间
 export namespace DepartmentSection {
    export class Section  {
        //===================Class Property by default public
        name: string;

        //==================Constructor of Section Class taking parameter of Employee Name
        constructor(theName: string) {

                this.name = theName;
            }
        //====================Function which displays the department name of a person
        Department(depatmentName: string = "") {
            console.log(`${this.name} ,  ${depatmentName} !!`);
        }

    }
      //============================================================================================
      //=========================Inheritance

    export class ITCell extends Section{
        constructor(SectionName: string) { 
            super(SectionName); 
        }
        DisplaySectionName(DepartmentName:string) {
            console.log("Printing Section name...");
            super.Department(DepartmentName);
        }

    }
     export class LoanAndAccount extends Section {
        constructor(SectionName: string) { 
            super(SectionName); 
        }
        DisplaySectionName(DepartmentName:string) {
            console.log("Printing another Section name...");
            super.Department(DepartmentName);
        }
    }

 }

我做错了吗?我试图像这样导入import DepartmentSection = require('./RajIsh');,但是当我尝试访问类和函数时,它会抛出错误Property 'ITCell' does not exist on type 'typeof' RajIsh。我该怎么办?

2 个答案:

答案 0 :(得分:2)

我在一段时间后遇到了同样的问题,我已经使用SystemJs解决了问题。尝试使用Systemjs。使用npm npm install systemjs从命令行安装SystemJs,并在head部分以这种方式在Index.html中实现

 <script src="node_modules/systemjs/dist/system.js"></script>

 <script>
    SystemJS.config({
    baseURL:"/", //Can write the path like /script and the browser will look inside the script folder
    packages:{
        ".":{
            defaultExtension:'js'
        }
    }
  })
  SystemJS.import("./main.js")
 <script>

试试这个应该运行。希望它可以帮到你。

答案 1 :(得分:1)

当您使用打字稿编译器将BOOL CALLBACK callback(HWND windowHandle, LPARAM parameter) { ... } ... EnumWindows(callback, reinterpret_cast<LPARAM>(&parameterData)); 个文件捆绑到一个.ts文件中时,您根本不需要main.jsimport

exportrequire仅在您使用模块捆绑包时才有效 - 它不包含在打字稿中!

您可以选择使用import来整理代码,但这不是必需的。

file thing.ts

namespace

file app.ts

namespace Test {
    class Thing {
    }
}

这是有效的,因为命名空间实际上在全局范围内,因此您可以从任何地方调用它们。像webpack这样的模块捆绑包会使您的代码保密(在全局范围内不可用),这就是您需要在webpack中使用namespace Main { class App { constructor(){ // you can make a new Thing instance here without import! let n = new Test.Thing() } } } import的原因。