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!

7

1 Answer 1

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.

6
  • 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
    Commented Aug 22, 2021 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
    Commented Aug 22, 2021 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 in Github
    – Dharmaraj
    Commented Aug 22, 2021 at 13:12
  • There are a few bugs with the types: github.com/berstend/puppeteer-extra/issues/433
    – Dharmaraj
    Commented Aug 22, 2021 at 13:24
  • Hmm pretty confusing. I guess I will use whatever import method works then. Thank you!
    – Christian
    Commented Aug 22, 2021 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.