【发布时间】:2019-01-22 14:06:59
【问题描述】:
我想将一个文本文件导入我的打字稿文件并简单地打印它。
index.ts 文件:
import d from "./a.txt";
console.log(d);
txt.d.ts 文件:
declare module "*.txt" {
const value: string;
export default value;
}
tsconfig.json 文件:
{
"compilerOptions": {
"module": "system"
}
}
package.json 文件:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"tsc": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"typescript": "^3.0.1"
}
}
但它给了我这个错误:
tsc "index.ts"
index.ts:1:15 - 错误 TS2307:找不到模块 './a.txt'。
1 import d from "./a.txt";
npm 错误!代码 ELIFECYCLE npm 错误! errno 2 npm 错误!测试@1.0.0 tsc:
tsc "index.ts"npm 错误!退出状态 2 npm ERR! npm 错误!失败于 test@1.0.0 tsc 脚本。 npm 错误!这可能不是问题 与 npm。上面可能还有额外的日志输出。npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!
C:\Users\SOstad\AppData\Roaming\npm-cache_logs\2018-08-15T16_45_37_115Z-debug.log
文件结构:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/15/2018 11:50 AM node_modules
-a---- 8/15/2018 12:40 PM 12 .gitignore
-a---- 8/15/2018 12:22 PM 20 a.txt
-a---- 8/15/2018 12:45 PM 112 index.js
-a---- 8/15/2018 12:25 PM 43 index.ts
-a---- 8/15/2018 11:50 AM 363 package-lock.json
-a---- 8/15/2018 11:51 AM 270 package.json
-a---- 8/15/2018 12:25 PM 59 tsconfig.json
-a---- 8/15/2018 12:25 PM 78 txt.d.ts
【问题讨论】:
标签: typescript import module