ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

ESM fails with protos which require other protos

Open AaronDewes opened this issue 4 years ago • 5 comments

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 avatar Aug 02 '21 16:08 AaronDewes

@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?

stephenh avatar Aug 03 '21 13:08 stephenh

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

AaronDewes avatar Aug 03 '21 13:08 AaronDewes

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...

stephenh avatar Aug 03 '21 15:08 stephenh

Hi there, any update here? Also struggling with this!

wtachau avatar Nov 04 '21 18:11 wtachau

Still an issue now in 2022 :)

paralin avatar Jun 13 '22 04:06 paralin

This can be closed with following: --ts_proto_opt=importSuffix=.js

Can you confirm?

@wtachau @paralin

leppaott avatar Sep 12 '22 06:09 leppaott

Yes, we added that flag for this reason.

paralin avatar Sep 12 '22 06:09 paralin

@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 avatar Sep 20 '22 20:09 crisog

@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

paralin avatar Sep 21 '22 00:09 paralin