Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing nodejs `fs` with typescript when executing with ts-node?

I'm trying to run the following code with ts-node.

import { writeFileSync, readFileSync } from 'fs'; 

However I get:

src/main/ts/create.ts (1,45): Cannot find module 'fs'. (2307) 

What do I need to do in to allow typescript to import the fs module?

like image 516
Ole Avatar asked May 06 '17 21:05

Ole


People also ask

Can you use fs in TypeScript?

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.

Does ts-node require TypeScript?

ts-node is a TypeScript execution engine and REPL for Node. js. It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node. js without precompiling.

Does ts-node use Tsconfig?

ts-node supports a variety of options which can be specified via tsconfig.


1 Answers

You need to run:

$ npm install @types/node --save-dev 

If you need additional information you can refer to the NodeJS QuickStart in the TypeScript Deep Dive by Basarat.

like image 182
Remo H. Jansen Avatar answered Sep 18 '22 11:09

Remo H. Jansen