Bug: Absolute paths using ES modules in Node
See original GitHub issueDescribe 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:
- Created 2 years ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Did you get any solution?
Yes, a PR would be great! Thanks for this hint! 👍🏻