找不到“未定义”中的模块

Cannot find module from "undefined"

本文关键字:未定义 模块 找不到      更新时间:2023-09-26

我正在尝试在我的Sublime Text 3中导入一个外部模块。外部模块 Ship.ts 如下:

export interface Ship {
    name: string;
    port: string;
    displacement: number;
}

output.ts 文件包含导入 Ship = require("./Ship"); 并且此文件保存和构建时没有错误。tsconfig.json 如下所示:

{
    "compileOptions": {
        "module": "commonjs"
    },
    "files": [
        "./Ship.ts"
    ]
}

但是,当查看内部和索引 php 页面时:

<html>
    <head>
        <script src="node_modules/commonjs-require/commonjs-require.js"></script>
        <script src="output.js"></script>
    </head>
    <body>
        Hello TypeScript
    </body>
</html>

。有一个控制台错误,指出commonjs-require.js:15 Uncaught Error: Cannot find module "./Ship" from "undefined" 。.ts 和 .js 文件以及 tsconfig.json 文件都位于同一目录中。任何帮助表示赞赏。

更新

即使我将 Ship.ts 的内容更改为:

export function greet(): void {
    console.log("Hello Ship");
} 

。转译为:

"use strict";
function greet() {
    console.log("Hello Ship");
}
exports.greet = greet;

。不幸的是,在尝试通过导入 Ship = require("./Ship") 或从"./Ship"导入 {Ship} 访问它后,错误消息 commonjs-require.js:15 Uncaught Error: Cannot find module "./Ship" from "undefined" 仍然打开。

更新

还试图在 output.ts 的顶部包含///<reference path="Ship.ts" />///<reference path="./Ship.ts" />,但无济于事。

您正在导出一个不生成任何代码的接口,并且您正在尝试在运行时要求它。