Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Absolute paths using ES modules in Node #99

Closed
vanntile opened this issue Apr 18, 2021 · 5 comments · Fixed by #133
Closed

Bug: Absolute paths using ES modules in Node #99

vanntile opened this issue Apr 18, 2021 · 5 comments · Fixed by #133
Labels
bug Something isn't working

Comments

@vanntile
Copy link

Describe the bug
In the module description, it says

Paths are relative to the root of the application folder

Using node ES modules in an express project, the paths are not relative, but absolute from the system root.

To Reproduce
The logger module file is below

import { Logger } from 'tslog'

const logger = new Logger({
  name: 'log',
  displayLoggerName: false,
  printLogMessageInNewLine: true,
  overwriteConsole: true,
})

export default logger

Using it like the following:

import logger from './utils/logger.mjs'

logger.info(`Connecting to ${config.PORT}`)

const err = new Error('Test Error')
logger.prettyError(err)

The output is

2021-04-18 06:56:20.482  INFO [file:///home/vanntile/src/tests/nn/src/app.mjs:12 undefined.<anonymous>]
Connecting to 8080 

 Error  Test Error
error stack:
• app.mjs:24 <anonymous>
    file:/home/vanntile/src/tests/nn/src/app.mjs:24:13

Expected behavior
The paths being relative from the project root.

Additional context
This happens both when running with node and nodemon.

Node.js Version
14.15.3

npm Version
7.9.0

OS incl. Version
Ubuntu 20.04

@ScorpionConMate
Copy link

Did you get any solution?

@vanntile
Copy link
Author

Did you get any solution?

No, I haven't tried anymore and moved to pino

@beanaroo
Copy link

I found that stackFrame.getFileName() at LoggerHelper.ts#L75 returns a path prefixed with file:// when using ES Modules.

Stripping it in cleanUpFilePath seems to restore functionality as documented.

If this is a possible solution, I'm happy to open a PR

@beanaroo
Copy link

I just noticed that Code Frame is also not printed when using ES Modules.

Modifying LoggerHelper.ts#L75 instead restores both relative paths and code frames:

    const filePath: string | null = stackFrame.getFileName().replace("file://", "");

@terehov
Copy link
Contributor

terehov commented Mar 14, 2022

Yes, a PR would be great! Thanks for this hint! 👍🏻

terehov added a commit that referenced this issue Mar 21, 2022
Fixes #99 - Handle ESM Stackframe Filepath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants