2
votes

I wanted to import a text file to my typescript file and simply print it.

index.ts file:

import d from "./a.txt";
console.log(d);

txt.d.ts file:

declare module "*.txt" {
  const value: string;
  export default value;
}

tsconfig.json file:

{
  "compilerOptions": {
    "module": "system"
  }
}

package.json file:

{
  "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"
  }
}

but it's giving me this error:

tsc "index.ts"

index.ts:1:15 - error TS2307: Cannot find module './a.txt'.

1 import d from "./a.txt";

npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! [email protected] tsc: tsc "index.ts" npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the [email protected] tsc script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR!
C:\Users\SOstad\AppData\Roaming\npm-cache_logs\2018-08-15T16_45_37_115Z-debug.log

file structure:

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
1

1 Answers

3
votes

Since you just run tsc index.ts, nothing is forcing txt.d.ts to be loaded. Given that you have a tsconfig.json file, you probably mean to run tsc -p .. This still won't work at runtime unless you're using a bundler that bundles the text file in the format you're assuming.