TypeScript - how to import 'os' module - node.js

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();

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.

'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.

Having trouble using/installing #pnp/sp modules

I'm trying to use the attachments module, specifically these features:
https://pnp.github.io/pnpjs/sp/attachments/
I've installed all pnp/sp modules necessary (I think!) using : https://pnp.github.io/pnpjs/getting-started/ as a reference.
Problem is I'm getting a 'cannot find module' for the attachments module. Below are the imports:
import { default as pnp } from 'sp-pnp-js';
import { ItemAddResult, Web } from 'sp-pnp-js';
import { sp } from "#pnp/sp"; //this is fine, which suggests it's installed properly?
import { IItem } from '#pnp/sp/attachments'; //cannot find this module
import "#pnp/sp/webs";
import "#pnp/sp/lists/web";
import "#pnp/sp/items";
import "#pnp/sp/attachments";
I've been successfully using pnp.sp features in this particular project so am stumped why I can't import and use the attachments feature.
If this is an existing project, that you've had for a bit, and you've already got #pnp in the package.json file, it may be you're using version 1 of #pnp, and you are reading the documentation for version 2.
I get the same error cannot find module when I add it to a project #pnp/sp 1.3.7
But don't get it with version 2.0.0
Version 1 document for attachments:
https://pnp.github.io/pnpjs/v1/sp/docs/attachments/
Version 2:
https://pnp.github.io/pnpjs/sp/attachments/

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

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'

Import native node modules correctly in Typescript

I've been using node for quite a while now (for my backends) and typescript with ionic (for frontend). On Ionic i realize I have managed to avoid many pitfalls and errors just because of TypeScript. I have hence decided to convert all my backends that are in pure JS to TypeScript.
The first obstacle I've run into is how to import native node modules like http, os and child_process, among others, correctly.
On most modules you can usually do something like import { some_export } from 'that_module'. I can also see the type definitions for node in the #types/ repo. I've tried import { http, os } from 'node' but I get a complaint that
/node_modules/#types/node/index.d.ts is not a module
My question hence is how should I import native node modules?
I've managed to solve this issue thanks to some light reading from this simple tutorial
To my understanding, the native modules are standalone modules that are not namespaced under node. You should therefore import from them directly.
Simply done so:
import * as http from "http";
import * as os from "os";
import * as path from "path";
.
.
.
and so on

Resources