0

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!

Christian
  • 834
  • 7
  • 18
  • Have you checked [require working but import not working](https://stackoverflow.com/questions/40762352/require-working-but-import-not-working)? – Dharmaraj Aug 21 '21 at 17:08
  • I just read through it. So the answer is, that the import statement depends on the exports of the module? – Christian Aug 21 '21 at 17:17
  • Yes, I wasn't able to find any example that user ES6 imports with puppeteer – Dharmaraj Aug 21 '21 at 17:19
  • Okay, so it's fine to mix them as long as it works? Do you have any advice on the other parts of the question? – Christian Aug 21 '21 at 17:27
  • Have you tried `import puppeteer from "puppeteer-extra"` ? – Dharmaraj Aug 21 '21 at 18:21
  • No, but that seems to work. So ```import puppeteer from "puppeteer-extra"``` is the same as ```const puppeteer = required("puppeteer-extra")``` in its functionality? – Christian Aug 22 '21 at 12:28
  • 1
    Checkout [The difference between require(x) and "import x"](https://stackoverflow.com/questions/46677752/the-difference-between-requirex-and-import-x). – Dharmaraj Aug 22 '21 at 12:40

1 Answers1

2

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.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • I just tried to convert my ts files to js with "tsc" and got the error ```Module '"puppeteer"' has no exported member 'FetcherOptions' export { FetcherOptions } from 'puppeteer';``` so ```import puppeteer from "puppeteer-extra"``` does not seem to work – Christian Aug 22 '21 at 12:46
  • @Christian Can you share a screenshot of error? Also can you hover on `puppeteer` in `import puppeteer from "puppeteer-extra"` and share what it is? It's interface. – Dharmaraj Aug 22 '21 at 12:51
  • @Christian looking into... I also got `'headless' does not exist in type 'LaunchOptions'` along with those 2 which apparently had an [issue](https://github.com/berstend/puppeteer-extra/issues/426) in Github – Dharmaraj Aug 22 '21 at 13:12
  • There are a few bugs with the types: https://github.com/berstend/puppeteer-extra/issues/433 – Dharmaraj Aug 22 '21 at 13:24
  • Hmm pretty confusing. I guess I will use whatever import method works then. Thank you! – Christian Aug 22 '21 at 13:31
  • @Christian so the correct way actually is to use `import puppeteer from "puppeteer-extra"` however there are some broken types.. I have tried using several older version but was not able to get a working combination. – Dharmaraj Aug 22 '21 at 13:32