Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find module "fs" in VS Code with TypeScript?

I'm running on a MacBook Air. I installed VS Code as an IDE and also have TypeScript installed.

I have a simple file with just this line:

import fs = require('fs'); 

I'm getting a red squiggly under the 'fs' inside the parenthesis and the error message is [ts] Cannot find module 'fs'. The file has a .ts extension. I'm new to JavaScript and to TypeScript, but I was under the impression that fs was a core module, so how could it not be found? How do I fix the problem?

Other things that I tried already:

  • Putting a simple function body in the file and then compiling on the command line with tsc. I get an essentially equivalent error there: error TS2307: Cannot find module 'fs'.
  • On the command line sudo npm install fs -g. This reports apparent success, but doesn't fix the problem.

I poked around SE and the web, but the answers that seemed close all appear to assume that 'fs' is available.

like image 856
Brick Avatar asked May 16 '16 18:05

Brick


People also ask

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.

How do I read a text file in TypeScript?

Use the readFileSync() method to read a file's contents in TypeScript, e.g. readFileSync(join(__dirname, 'example. txt'), 'utf-8') . The method takes the path and encoding as parameters and returns the contents of the specified file.

Can I use fs in Javascript?

With Node. js, you can programmatically manipulate files with the built-in fs module. The name is short for “file system,” and the module contains all the functions you need to read, write, and delete files on the local machine.

Is fs included in node?

fs it's a native node. js module, you don't need install it.


2 Answers

You need to include the definition file for node.

TypeScript 2.0+

Install using npm:

npm install --save-dev @types/node 

TypeScript < 2.0

If you use typings then you can run this command:

typings install dt~node --global --save 

Or if you are using typings < 1.0 run:

typings install node --ambient --save 

Or if all else fails, manually download the file here and include it in your project.

like image 153
David Sherret Avatar answered Oct 19 '22 23:10

David Sherret


There is a better way now without going to the previous tsd or typings tools. NPM now has the @types package for typescript. In this example you need the package @types/node:

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

Make sure you are using the save-dev option to only install the types in development mode, not in production. You should have the latest node libraries when use the npm install "@types/" syntax...

It not finding the fs package because the previous tools typings most likely not using the latest node.d.ts definition file.

Your tsconfig.json file needs to be updated to find these type packages. My example if using jquery, jqueryui and node types. Assuming you need the syntax to work for your code editor as well, in this case the 'atom' code editor

{ "compileOnSave": false, "compilerOptions": {     "rootDir": "src",     "sourceMap": true,     "target": "es5",     "module": "amd",     "declaration": false,     "noImplicitAny": false,     "removeComments": true,     "emitDecoratorMetadata": true,     "experimentalDecorators": true,     "moduleResolution": "node",     "lib": ["es2015", "dom"],     "baseUrl": "./",     "typeRoots": [         "node_modules/@types"     ],     "types": [         "jquery",         "jqueryui",         "node"     ],     "paths": {         "src/*": ["src/*"]     } }, "exclude": [     "node_modules",     "dist",     "build" ], "filesGlob": [     "./src/**/*.ts",     "./test/**/*.ts",     "./typings/index.d.ts",     "./custom_typings/**/*.d.ts",     "./node_modules/@types/**/*.d.ts" ], "atom": {     "rewriteTsconfig": false } } 
like image 30
Abe Avatar answered Oct 19 '22 22:10

Abe