找不到“未定义”中的模块
Cannot find module from "undefined"
我正在尝试在我的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" />
,但无济于事。
您正在导出一个不生成任何代码的接口,并且您正在尝试在运行时要求它。
相关文章:
- 未定义不是函数:模块导出
- 未定义应为节点模块的对象
- Nodejs模块函数返回未定义的,而不是异步的
- Visual Studio 2015 Update 2-'中的TypeScript模块;要求'未定义
- 从模块函数返回未定义的对象
- '未定义'当需要模块时输出
- 模块模式:函数未定义
- 使用es6模块:未捕获ReferenceError:define未定义
- 找不到“未定义”中的模块
- 茉莉花测试显示模块未定义
- 揭示模块模式:对象未定义
- 使用 NodeJS 的请求模块进行 HTTP 身份验证,返回未定义的正文
- Typerror:mongo 模块的 find 方法是未定义的
- 节点.js模块级变量未定义
- 当未定义默认导出时,从“模块”导入模块是什么,为什么它与将 * 导入为模块不同
- 自定义模块:未捕获类型错误:未定义不是一个函数
- Node 的加密模块是否阻止,即使对于未定义为同步的方法也是如此
- 未捕获的引用错误:未定义模块 A
- 需要 JS 未定义模块
- 未捕获的ReferenceError:未定义模块RequireJS