node.js - 如何通过相对路径导入非 TypeScript 模块?
问题描述
我有以下文件结构:
.
├── a
│ ├── package.json
│ ├── src
│ │ └── main.ts
│ └── tsconfig.json
└── b
├── package.json
└── index.js
a
是一个 TypeScript 模块,b
是一个纯 JavaScript 模块。
a/src/main.ts 包含以下代码:
import {helloB} from "../../b";
console.log("Hello from A!");
helloB();
b/index.js 包含以下代码:
module.exports.helloB = function() {
console.log("Hello from B!");
};
我想使用相对导入来导入b
。a
我已经尝试../../b
按照上面指定的方式导入,但出现以下错误:
src/main.ts:1:22 - error TS6059: File '/home/ivanq/Documents/tstest/b/index.js' is not under 'rootDir' '/home/ivanq/Documents/tstest/a/src'. 'rootDir' is expected to contain all source files.
1 import {helloB} from "../../b";
我可以添加b
,rootDir
但我不希望 TypeScript 编译该模块并将其输出到构建目录。我希望它require("../../b")
在运行时执行,就像在node_modules
. 我如何配置 TypeScript 来做到这一点?
这是我的tsconfig.json
:
{
"compilerOptions": {
"allowJs": true,
"outDir": "build",
"rootDir": "src",
},
"include": ["src"]
}
解决方案
推荐阅读
- python - 使用 FastCGI 在 IIS 中部署 Python Web 应用程序(Flask)得到 500 内部服务器错误
- postgresql - 具有多个唯一约束的 Postgresql UPSERT
- ajax - 使用 ajax/codeigniter 调用 url 时出现错误 404
- php - HTML5 .MP4 视频不播放
- node.js - 在nodejs中安装opencv时出错?
- ios - 在没有系统警报的情况下设置 NEHotspotConfiguration
- python - 从 Python 列表中过滤非英语关键字
- rspec - 页面同时具有可见和不可见的字段
- vb.net - 值不能为空。参数名称:objectType
- ssis - SSIS OLE DB 命令