question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Absolute paths using ES modules in Node

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ScorpionConMatecommented, Feb 21, 2022

Did you get any solution?

0reactions
terehovcommented, Mar 14, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Are Native Node Absolute (ie. Root-Relative) Paths Possible ...
node -r esm ): NODE_PATH doesn't work with native ES Modules (ie. adding "type": "module" to package.
Read more >
Alternatives to __dirname in Node.js with ES modules
ES modules with Node.js add a new, standardized global that allows your code to run anywhere, locally or remotely.
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
Node.js has two module systems: CommonJS modules and ECMAScript modules. ... A required module prefixed with '/' is an absolute path to the...
Read more >
Documentation - Module Resolution - TypeScript
The full Node.js resolution algorithm is outlined in Node.js module ... Node will walk up the directory chain, looking through each node_modules until...
Read more >
rollup.js
To make sure your ES modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found