Directory import is not supported resolving ES modules when trying import { map } from 'rxjs/operators'; - node.js

I'm getting the following error when trying to import the map operator.
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import is not supported resolving ES modules imported from
import { map } from 'rxjs/operators';
Node version: v14.15.0

It seems that ES6 modules are not yet supported
To be able to import the map module I needed to use internals as follow:
import { map } from 'rxjs/internal/operators/map.js';
Hope this might help someone.

Related

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

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

Importing DocumentReference in Es6

Classes like DocumentReference, CollectionReference, etc., don't seem to be exported. Is there a way to import these classes?
Install firebase if it's not in your package.json:
npm install --save firebase
In the .ts file just import whatever you need:
import * as firebase from 'firebase/app'
import DocumentReference = firebase.firestore.DocumentReference
Use DocumentReference:
posts.forEach((doc: DocumentReference) => {
doc.ref.delete()
})
using typescript, this works:
import * as firebase from "firebase"
then do
firebase.firestore().DocumentReference

Import unqualified name in Node.js

In Python I can import the names in my module as qualified
import myModule
or unqualified
from myModule import *
In Node.js, I can import as qualified
var myModule = require('myModule')
Is there a way to import names unqualified?
You can do that in ES6: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
ES6 imports are not supported in node.js at this point though. You could get it by writing ES6 code and transpiling it with babel, for example https://babeljs.io/

Resources