Currently, I am in the process of developing an npm module that serves as a basic logger. The initial implementation was done using plain JS:
logger.js
class Logger {
// additional code
}
module.exports = Logger;
The above code works seamlessly in a JavaScript script like so:
logger-test.js
const Logger = require('ep-logger');
const logger = new Logger('my-logger');
logger.info('yo');
However, the team at our company predominantly uses TypeScript. To adapt to this, alongside my logger.js
, I also created a file named logger.d.ts
with the following structure:
export interface SplunkOptions {
endpoint?: string
loggingSource?: string
loggingSourceType?: string
}
export interface LoggerOptions {
stage?: string
level?: string
splunk?: SplunkOptions
}
export default class Logger {
constructor(name: string);
public static setOptions(obj: LoggerOptions): void;
public error(message: string | Error, ...data: any[]): void;
public warn(message: string | Error, ...data: any[]): void;
public info(message: string | Error, ...data: any[]): void;
public verbose(message: string | Error, ...data: any[]): void;
public debug(message: string | Error, ...data: any[]): void;
public silly(message: string | Error, ...data: any[]): void;
}
To align with TypeScript requirements, I made changes in my package.json
:
{
// more package.json configurations go here
"main": "src/logger.js",
"types": "src/logger.d.ts"
}
When attempting to use the logger in my TypeScript project, I encountered an issue:
index.ts
import Logger from 'ep-logger';
const logger: Logger = new Logger('my-logger');
logger.info('yo');
An error message indicating absence of default export for the logger arose:
{ Error: Command failed: ts-node local-lambda.ts --colors
/Users/luke/Arbeit/WeltN24/Editorial Products/projects/ep-logger-tests/js-app/yo-lambda/src/index.ts:5 const logger: Logger = new Logger('my-logger'); ^ TypeError: ep_logger_1.default is not a constructor ...
In order to investigate further, I executed
console.log(Logger);
And the output was
undefined