Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Node.js simplest setup doesn't work -- error TS2307: Cannot find module 'fs'

I've installed TS and the node typings globally:

PS C:\Projects\Test> npm list --global --depth=0
C:\Users\Jan\AppData\Roaming\npm
+-- @types/[email protected]
+-- [email protected]
`-- [email protected]

Then I've created a file test.ts

import fs = require("fs");
let text = fs.readFileSync("myFile.txt");
console.log(text);

Runnning tsc results in

PS C:\Projects\Test> tsc .\test.ts
test.ts(1,21): error TS2307: Cannot find module 'fs'.

Do I miss something obvious?

Thanks!

like image 383
Knack Avatar asked Feb 24 '17 08:02

Knack


People also ask

Can t find module fs?

To solve the "Cannot find module fs or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import fs with the following line of code import * as fs from 'fs' .

What is TS2307?

tsc throws `TS2307: Cannot find module` for a local file.

How do I use TypeScript fs?

To import and use the fs module in TypeScript, make sure to install the type definitions for node by running npm i -D @types/node and import fs with the following line import * as fs from 'fs' , or import { promises as fsPromises } from 'fs' if using fs promises.

Can not find angular module?

To solve the error "Cannot find module '@angular/core'", make sure you have installed all dependencies by running the npm install command, set the baseUrl option to src in your tsconfig. json file and restart your IDE and development server.


1 Answers

You should install @types locally to your project.

npm install @types/node --save-dev

TypeScript will not find globally installed types as definition files.

like image 89
Herrington Darkholme Avatar answered Jan 02 '23 07:01

Herrington Darkholme