node.js - 需要一个包含index.js的文件夹

标签 node.js typescript

我尝试通过指定文件夹名称来请求包含 index.js 的文件夹

import xyz = require("./xyz");

并收到编译错误“无法解析外部模块 xyz。模块无法别名为非模块类型。

如果我指定索引

import xyz = require("./xyz/index");

它有效。

如果我这样做

var xyz = require("./xyz");

它有效,但我在 xyz 上没有智能感知。

Typescript 不支持该功能吗?

最佳答案

不,TypeScript 不遵循 Node.js 的模块解析规则,因为它们非常特定于该平台。 TypeScript 支持不遵循 Node.js 约定的多个平台。

TypeScript 规范规定,关于相对模块解析:

If the import declaration specifies a relative external module name, the name is resolved relative to the directory of the referencing module’s file path. The program must contain a module with the resulting file path or otherwise an error occurs. For example, in a module with the file path ‘C:/src/ui/main’, the external module names ‘./editor’ and ‘../lib/io’ reference modules with the file paths ‘C:/src/ui/editor’ and ‘C:/src/lib/io’.

关于node.js - 需要一个包含index.js的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323956/

相关文章:

node.js - Jest - 包装在 promisify 中的模拟 zlib 函数不起作用

javascript - 在 GraphQL 中上传图片

javascript - 如何模拟 firebase.auth.UserCredential Jest ?

typescript - 如何在 typescript 中提取函数的类型?

javascript - 必须正确处理 Promise 错误

arrays - 如何增加数组mongodb下重复值的计数器

Javascript:如何模拟浏览器 cookie 的实现?

node.js - 从 Electron 打包应用程序运行 Cli 命令

angular - rxjs 限制 this.durationSelector 不是函数

angular - 如何提供不同的模拟服务来测试 Angular 2 组件?