ts-proto
ts-proto copied to clipboard
ESM fails with protos which require other protos
I'm using this library with lnd's rpc.proto, stateservice.proto and walletunlocker.proto and the esModuleInterop flag.
Because walletunlocker.proto uses ChanBackupSnapshot from rpc.proto, it adds the following line
import { ChanBackupSnapshot } from "./rpc";
For a generated file to be true ESM and work however, it needs to be
import { ChanBackupSnapshot } from "./rpc.js";
Because TypeScript can still resolve the import with .js, but node needs the full path with extension in the output.
The same applies for
Current:
import _m0 from "protobufjs/minimal";
Working:
import _m0 from "protobufjs/minimal.js";
@AaronDewes as far as I understand, including the file extension in imports has historically been not recommended.
Is there something specifically different about Ind and/or your setup? I.e. are their multiple rpc.js / rpc.cjs / rpc.mjs such that rpc is ambiguous?
Sorry, I've configured type: module in my package.json and made tsc output module code (instead of using esModuleinterop in tsconfig.json). When being run in an ESM context, Node.js seems to require file extensions. Here's the repo with the code I'm using: GitHub.com/runcitadel/middleware
Oh wow...you're right:
https://nodejs.org/api/esm.html#esm_mandatory_file_extensions
Huh. I guess we could add another option, like --includeFileExtensions ... or the file extensions don't hurt in non-ESM mode, so maybe we should always do it...
Hi there, any update here? Also struggling with this!
Still an issue now in 2022 :)
This can be closed with following:
--ts_proto_opt=importSuffix=.js
Can you confirm?
@wtachau @paralin
Yes, we added that flag for this reason.
@paralin I'm using --ts_proto_opt=esModuleInterop=true --ts_proto_opt=importSuffix=.js and I still get imports without the suffix .js. Any ideas?
import * as _m0 from 'protobufjs/minimal'
@crisog check the example here: https://github.com/aperturerobotics/starpc/blob/ffdea3a0b224b3527e9e9d2193cba55c783112b1/Makefile#L106
Does it work? I'll try to double check this evening