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?
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.
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.
ts-node supports a variety of options which can be specified via tsconfig.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With