grpc-node
grpc-node copied to clipboard
Generated proto types are not valid for ES modules
Environment
- Mac m1 2020
- Node 18.14.2
- nvm
- @grpc/grpc-js: "^1.8.13",
- @grpc/proto-loader: "^0.7.6"
Problem description
The generated file(s) are not compatible with ES modules.
Reproduction steps
- Package.json with
"type": "module"
- tsconfig with
"module": "node16", "moduleResolution": "node16"
- generate types using command:
proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=src/generated src/.proto
How to Fix
The generated import should end with ".js": (See image below)
import type { BookItem as _bookStorePackage_BookItem, BookItem__Output as _bookStorePackage_BookItem__Output } from '../bookStorePackage/BookItem.js';
Additional context
Current error:
error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './bookStorePackage/Book.js'?
Proto file:
syntax = 'proto3';
package bookStorePackage;
service Book {
rpc createBook (BookItem) returns (BookItem);
}
message BookItem {
int32 id = 1;
string book = 2;
}
Image with error:
好像不能用import都要用require
Note that the file is auto-generated. It should not be modified manually
I am having the same issue in Deno. It is very problematic because the ESM module specification explicitly disallows imports without the file extension, so there are a lot of environments that do not support these imports.
This should be reopened as the fix was reverted. This needs to be adressed somehow as esm won't go anywhere :)
Thanks, I'll take a look at the reasons for the revert as soon as I have a chance and see if I can help out somehow.