Typescript Cannot find Module "fs" even though #types/node installed - node.js

I know the usual fix for this one is to install #types/node , and I have 10.12.23 of that installed.
This appears to be a strange error and I am a bit baffled by it. I have 2 other npm modules installed: config ( which requires #types/config ) and firebase-admin which must have it's own typescript types. Also using VS code version 1.31 . I even tried installing an older version of #types/node
The following works fine
import admin from "firebase-admin";
import fs from "fs";
The following fails: cannot find module 'fs'
import admin from "config";
import fs from "fs";
The following fails: cannot find module 'fs'
import fs from "fs";
I am not using any other packages / webpack or anything else. Any ideas are appreciated.

Besides installing #types/node, check that the tsconfig.json:
does not have a compilerOptions types, or make sure "node" is included there
noResolve does not exist or is set to false
esModuleInterop is set to true or you'll get error Module "fs" has no default export

Import fs in your ts/js file
import fs from 'fs'

Related

mockingoose does not work with TypeScript

The following import
import mockingoose from 'mockingoose';
gives an error Could not find a declaration file for module 'mockingoose'. with TypeScript.
According to this npm page,
mockingoose provides its own type definitions, so you don't need #types/mockingoose installed!
I'm not sure how to fix this.

Module not found: Can't resolve 'aws-sdk' when import nodejieba to react

I run into this error when my script import nodejieba.
It named 「Module not found: Can't resolve 'aws-sdk' in 'C:\Users\user\Documents\cchatbigdata\node_modules#mapbox\node-pre-gyp\lib\util'」
At first, the react run well and my script work fine.
then I npm install nodejieba --save-dev
and add import nodejieba from 'nodejieba' in my TestedData.js.
even if I don't use nodejieba's function, my react ran into this error.
I start to search related question about Can't resolve 'aws-sdk'.
most of them encountering this error when using webpack, but I don't use webpack, I just import nodejieba.
I try to understand node-pre-gyp\lib\util under the node_modules.
but it created by React(I using npx create-react-app), so I think if changing the file react may shutdown.
I think something wrong between react and nodejieba.
My browser show those errors, I found they all related to node-pre-gyp\lib\util.
./node_modules/#mapbox/node-pre-gyp/lib/util/s3_setup.js
Module not found: Can't resolve 'aws-sdk' in 'C:\Users\kevin\Documents\cchatbigdata\node_modules\#mapbox\node-pre-gyp\lib\util'
console.<computed> # index.js:1
handleErrors # webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage # webpackHotDevClient.js:213
index.js:1
./node_modules/#mapbox/node-pre-gyp/lib/util/s3_setup.js
Module not found: Can't resolve 'mock-aws-s3' in 'C:\Users\kevin\Documents\cchatbigdata\node_modules\#mapbox\node-pre-gyp\lib\util'
console.<computed> # index.js:1
handleErrors # webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage # webpackHotDevClient.js:213
index.js:1
./node_modules/#mapbox/node-pre-gyp/lib/util/s3_setup.js
Module not found: Can't resolve 'nock' in 'C:\Users\kevin\Documents\cchatbigdata\node_modules\#mapbox\node-pre-gyp\lib\util'
console.<computed> # index.js:1
handleErrors # webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage # webpackHotDevClient.js:213
Maybe node-pre-gyp under the node_modules must to be fixed.
Here is my TestData.js
import {useState, useEffect} from 'react';
import nodejieba from 'nodejieba'; //this line make me run into this error!
...
Thank for your reading and help!
Run npm install util
I don't believe this error has anything to do with nodejieba. You used npx create-react-app and it must be using Webpack 5 now, which does not include polyfills for node.js anymore. An alternate solution is to use https://www.npmjs.com/package/node-polyfill-webpack-plugin but I liked installing just the single util package for my use case.

'ReferenceError: require is not defined' while importing fs module

I am new to NodeJS. I was importing the 'fs' module in NodeJS and this happened.
Is this because of the new import syntax in the current versions?
What went wrong?
Thanks in advance!
This is because something is telling nodejs that this is the newer ESM module. This could be your package.json file or something else. In an ESM module file, you use import, not require() to load modules.
You can see in the stack trace where it shows Object.loadESM and that's how you know it is trying to load this module as an ESM module.
With an ESM module, perhaps you want this:
import fs from "fs";
or
import * as fs from'fs';
Or, if you intend to use a CommonJS module instead (where you can use require()), then we need to see your package.json file to figure out why the loader is attempting to load your file as an ESM module.

TypeScript - how to import 'os' module

I'm learning Typescript. To do this, I'm building a basic utility app with Node for myself. For this app, I need to use Node's OS Module. My question is, how do I import this module?
In my Typescript file, I have the following:
import { os } from 'os';
This line generates the error: "Cannot find module 'os'". What am I missing?
This line generates the error: "Cannot find module 'os'". What am I missing?
The correct code is
import os from 'os';
Also make sure you have npm i #types/node
More
Some notes I wrote on NodeJS quickstart : https://basarat.gitbook.io/typescript/docs/quick/nodejs.html
Just to update this entry:
import * as os from 'os';
and later, you can use:
const hostname = os.hostname();

Why am I getting a "Cannot find module" err when compiling Typescript?

I am just getting started using typescript on the server and I'm pretty much stuck trying to import 3rd party npm modules. Here is what I have declared:
import mongodb = require('mongodb');
import assert = require('assert');
import Q = require('q');
... and I am getting the following errors when compiling:
src/Databse.ts(1,26): error TS2307: Cannot find module 'mongodb'.
src/Databse.ts(2,25): error TS2307: Cannot find module 'assert'.
src/Databse.ts(3,21): error TS2307: Cannot find module 'q'.
What is the correct way to import 3rd party modules?
It doesn't feel the correct way to import 3rd party's library. You can simply use only import
import "library";
OR
import {module} from "library";
Note:- when you are using above syntax then you need to make your that
the module you are importing exist otherwise you will get the error
that it can't find the module you are trying to import.
If you want to use webpack then
require("library");
Or like this
var module = require("library");
Have you installed the typing declarations ?
typings install mongodb --save
typings install dt~assert --save
typings install dt~q --save
Then you will need to reference the typings.
Either using the triple slash-directive
/// <reference path="..." />
More about it here Triple-Slash Directives
Or add the typings index.d.ts file in files array in tsconfig.json
"files": [
"./typings/index.d.ts",
]

Resources