[BUG] `tslog` loads Node.js runtime in a browser environment when used as a transitive dependency
Describe the bug
I created a logging library that uses tslog under the hood. The library is compiled with tsc (there's no bundling) and shipped as a CommonJS package. It works well in a Node.js environment, but it fails in the browser with the error: TypeError: (0 , util_1.formatWithOptions) is not a function.
I'm not entirely sure what happens, but I suspect the following "browser" entry from tslog's package.json is being ignored:
"browser": {
"tslog": "./dist/browser/index.js",
"./dist/esm/runtime/nodejs/index.js": "./dist/esm/runtime/browser/index.js",
"./dist/cjs/runtime/nodejs/index.js": "./dist/cjs/runtime/browser/index.js",
"./src/runtime/nodejs/index.js": "./src/runtime/browser/index.js",
"util": false
}
As a result, the Node.js runtime is being used in the browser.
To Reproduce
- Create a package that uses
tslog. - Compile it with
tscas a CommonJS module. - Publish the package and use it as a dependency in an app running in a browser environment.
- Instantiate the logger and call any logger method, such as
logger.info().
Expected behavior
The browser version of tslog should be used, without errors, in browser environments.
Screenshots
None
Additional context
I'm not sure if this is a bug in the library or if it's related to the fact that I'm shipping my module as CommonJS, where tsc converts tslog imports into require('tslog'). If that's the case, please treat this bug report as a support ticket. Thanks!
Node.js Version
v18.14.2
OS incl. Version
Any
met the same problem
It seems that this library has not been adequately tested in a browser environment, because as far as I know, util.formatWithOptions is a method in the Node.js environment.
Can I submit a PR to try to catch this error and provide better user feedback? Such as
'util.formatWithOptions' cannot work on browser, please overwrite the default 'transportFormatted' behavior. See https://tslog.js.org/#/?id=overwriting-default-behavior