For our backend nodeJS (typescript) project, we need to utilize the 'parse-domain' package (version 7.0.1). However, we encountered an issue where this package is not compatible with commonJS, making it unable to be loaded using require
. The workaround involved writing a non-async wrapper function that uses the import
function:
public parserDomain(url:string)
{
let pd = null;
async () => pd = await import('parse-domain');
return pd.parseDomain(url);
}
console.log(parserDomain("http://any.company.com"))
The problem arises when pd
remains as null
when the code reaches the return
operation inside the function.
How can we create a non-async function that successfully calls the parseDomain()
in this scenario?
Below is our tsconfig.json setup:
{
"compilerOptions": {
"noImplicitAny": false,
"strictNullChecks": false,
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"lib": [ "esnext", "dom" ],
"target": "es2015",
"strict": true,
"outDir": "./build/src",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap" :true,
"typeRoots": [ "./node_modules/@types" ],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types" : [ "node" ]
},
"include": ["src/**/*", "test/**/*"],
"exclude": [ "node_modules", "**/*.integration.ts" ]
}
And here are the relevant lines from our package.json file (nodeJS version: 20.4.0):
"parse-domain": "7.0.1",
"typescript": "^5.1.6"