In a Firebase Cloud Function project...
I have the following typescript file at the root of my src
directory right along side of my mai
The top-level import (import BcryptTool from './bcrypt.class';
) will automatically import the default
export from the bcrypt.class
module. However, when using the import statement as a function (so called "dynamic import"), it will import the module itself, not the default export.
You can see the difference when you would console.log(BcryptTool)
both imports:
import BcryptTool from './bcrypt.class'
will show { default: { [Function: BcryptTool] hashValue: [Function], compare: [Function] } }
const BcryptTool = await require('bcrypt.class')
will show { [Function: BcryptTool] hashValue: [Function], compare: [Function] }
Did you notice the default
in the first console.log? That shows you imported the module, not the default.
Now actually the import BcryptTool from './bcrypt.class'
syntax is syntactic sugar for doing import { default as BcryptTool } from './bcrypt.class'
. If you apply this knowledge on the dynamic import, you could do this:
const BcryptToolModule = await import('./bcrypt.class');
BcryptToolModule.default.compare(...);
Or in a cleaner syntax:
const { default: BcryptTool } = await import('./bcrypt.class');
BcryptTool.compare(...);