How to import node module in Angular? - node.js

I'm developing an Angular app.
For some reason I want to use querystring.escape().
I wrote import { escape } from 'querystring', but I got querystring.escape is not a function error.
How can I import a node module in typescript?

You are destructuring querystring trying to get the escape property, but then you want to call querystring.escape() which you haven't imported.
What you want to do is this:
import * as querystring from 'querystring';
or
import querystring from 'querystring'

Related

How to import interface from package

I'd like to import the Schema$ListMessagesResponse interface so I can validate the response that's returned.
I tried import { google, Schema$ListMessagesResponse } from 'googleapis'; but that doesn't work. I'm not familiar with importing this object from the package. It's documented here and here - How do I properly reference it?
https://googleapis.dev/nodejs/googleapis/latest/gmail/interfaces/Schema$Message.html
Node v12
TypeScript 4.3.5
You need to respect the given namespace gmail_v1 exporting the interface
try:
import {gmail_v1} from 'googleapis';
const messages: gmail_v1.Schema$ListMessagesResponse = //...

How to import module based dependencies for firebase cloud functions?

I want to organize my firebase cloud functions in specific files,
and currently, I have these 3:
index.ts
crypto.ts
webscrape.ts
Inside of these files, I have functions that use specific dependencies that are needed nowhere else.
For example, in crypto.ts I need the crypto-js package to encrypt some user data and store it into the database.
So I am importing it like so:
import * as CryptoJS from "crypto-js";
as advised in https://firebase.google.com/docs/functions/handle-dependencies#typescript
On the other hand, when I try to import puppeteer into webscrape.ts like this:
import * as puppeteer from"puppeteer-extra";
then calling puppeteer.launch(); gives me an error :
Property 'launch' does not exist on type 'typeof import("c:/Users/username/Desktop/project/firebasee/functions/node_modules/puppeteer-extra/dist/index")'
and it only works when I do const puppeteer = require("puppeteer-extra");
What's the difference here?
My goal is to keep the dependencies of each functions and file/module as small as possible because I assume that this will also keep the size of each function container small (Is that even true?)
I didn't want to import everything to index.ts even when I trigger a function, that doesn't use this dependency at all.
So what is the correct way of handling these dependencies?
Thanks!
The following import will get the default export from that package.
import puppeteer from "puppeteer-extra"
I looked for the default export in the Github repository and found that.
const defaultExport: PuppeteerExtra = (() => {
return new PuppeteerExtra(...requireVanillaPuppeteer())
})()
export default defaultExport
They have mentioned both ES6 import and require methods here.
// javascript import
const puppeteer = require('puppeteer-extra')
// typescript/es6 module import
import puppeteer from 'puppeteer-extra'
You can read more about import on MDN.

unable to figure out error from passport-custom

trying to use passport-custom and the very first line, from pseudocode at npmjs, errors out:
import passportCustom from 'passport-custom';
The is no default import in index.js when I open it up under node_modules/passport-custom/lib
I must be missing something fundamental here, don't know what though
Try to use CommonJS const passportCustom = require("passport-custom") You probably have older version of Node.js which does not support ES6 modules.
There is no default export. So you will have to name the items you want to import (put them in curly braces).
//Example:
import { a,b,c,d} from 'youPackage';
//Your case:
import { passportCustom } from 'passport-custom';
Above are called named imports. When a package exports one item by default using: export default passportCustom ;, you could have use your code. You can access the code of the package to have a look for yourself.

How to use declare functions in NestJS

I was using this repo with Node.js:
https://www.npmjs.com/package/pg-essential
But now, with NestJS I'm having problems to make the import properly.
The original lines are:
var pg = require('pg');
require('pg-essential').patch(pg);
And using NestJS I can use: import * as pg from 'pg';
for the pg require, but how can import the second require?
You could do
import { patch } from 'pg-essential';
patch(pg);
Importing a named exporting and calling the function, instead of doing a chained require function.

What makes a typescript module, a typescript module? Toastr example

I was just using toastrjs for a few notifications, and I ran into this little problem. Ideally, when you import a library in nodejs, you have to make an import statement, like so:
import http = require("http");
However, when I tried this with toastr, I get an error, even after including the reference path. So, something like this:
///<reference path='toastr.d.ts' />
import toastr = require("./toastr");
I get this error:
error TS2071: Unable to resolve external module '"./toastr.js"'.
error TS2072: Module cannot be aliased to a non-module type.
How is toastr different from a regular node module like http?
Update 1
I tried to do the same thing with jQuery but I have the same problems, does this mean that this does not work with frameworks that are designed to be client-side?
the following declare definition would create a module you can import via amd/commonjs:
declare module "jquery"{
export var jQuery: JQueryStatic;
}
Then you can do:
import jquery = require("jquery");
You can see such definitions in this underscore definition: https://github.com/borisyankov/DefinitelyTyped/blob/master/underscore/underscore.d.ts#L2853
or node.d.ts : https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node.d.ts#L203
However not all files on DT have this definition. As it is simple enough to add on your own and you are free to name these modules whatever you want (in your AMD configuration http://www.youtube.com/watch?v=4AGQpv0MKsA )

Resources