node-config icon indicating copy to clipboard operation
node-config copied to clipboard

Support Bun + Typescript

Open akefirad opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe. Does node-config support Bun? If not, any plan to support it? I'm not sure if this is a bug or lack of feature, I saw in one old issue people mentioned some failure while using node-config in Bun, but I suspect it's not in TS.

Describe the solution you'd like I'd like to use node-config in Bun in TS. The current codebase works flawlessly with node-config in TS in Node. Migrating to Bun, it fails, complaining that it couldn't find ts-node in node_modules. (Looking at the code, this is not surprising since the parser for .ts files is indeed ts-node.)

Describe alternatives you've considered Right now I converted all files to JSON, but the default file contains some logic (e.g. reading secrets from AWS SecretManager) which can't be converted to JSON. Converting it to JS does seem to be working. But it's a bit off to have a single JS file in a fully TS codebase.

Please tell us about your environment:

  • node-config version: 3.3.12
  • node-version: #N/A
  • bun-version: 1.1.18

Thanks.

akefirad avatar Jul 07 '24 09:07 akefirad

I have not used Bun, but my guess is it'll work if you add ts-node to your package.json dependencies.

By default, node-config only loads a couple of parsers - JSON and JSON5. Every other parser has to be specified as a dependency in your package.json file. This is so every app doesn't download every parser that has ever been integrated.

There's a short mention of this in the docs, but documentation is for wimps ;)

lorenwest avatar Jul 08 '24 02:07 lorenwest

Right, but that's kinda the point; if people use Bun then they don't have Node and (I'm guessing that) ts-node is not an option for them. So the idea is to use Bun instead of ts-node as a Parser. (I'm not sure if ts-node can utilize Bun instead of Node.) Does that make sense?

akefirad avatar Jul 08 '24 15:07 akefirad

None of this makes sense. If you're not using node.js for node-config, you're on your own.

lorenwest avatar Jul 08 '24 20:07 lorenwest

that seems to be more a bun issue than a lib issue.

nextor2k avatar Jan 29 '25 21:01 nextor2k

I also use bun but my config files are .js indeed.
Maybe this could be fixed by overriding the Parser.tsParser with Parser.jsParser in your code?
You can check process.versions.bun to know if you're running bun or not.

Telokis avatar Feb 17 '25 10:02 Telokis

The bun documentation claims it supports ts files natively.

https://github.com/oven-sh/bun/blob/main/docs/runtime/typescript.md

@akefirad is talking about: https://github.com/node-config/node-config/blob/4d7fac6104a7e984557894e7edf2077890509c6a/parser.js#L71

Node 23 supports ts

Node 22:

require.extensions [Object: null prototype] { '.js': [Function (anonymous)], '.json': [Function (anonymous)], '.node': [Function (anonymous)] }

Node 23

require.extensions [Object: null prototype] { '.js': [Function (anonymous)], '.json': [Function (anonymous)], '.node': [Function (anonymous)], '.cts': [Function: loadCTS], '.ts': [Function: loadTS], '.mts': [Function: loadMTS] }

Bun 1.2:

require.extensions { '.js': true, '.json': true, '.node': true }

So Bun needs a bug report to fix this.

jdmarshall avatar Feb 28 '25 07:02 jdmarshall

Simplest solution if you know you're working in Bun might be to

require.extensions['.ts'] = true

jdmarshall avatar Mar 06 '25 01:03 jdmarshall

https://github.com/oven-sh/bun/pull/18478

jdmarshall avatar Mar 27 '25 22:03 jdmarshall

I'm narrowing the scope of this to specifically consider Bun, and closing it as a bug on Bun's side that's getting resolved as it gains support for require.extensions

markstos avatar Apr 07 '25 20:04 markstos

The linked bun ticket is now marked as closed, fixed.

jdmarshall avatar Oct 09 '25 23:10 jdmarshall