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

Generated proto types are not valid for ES modules

Open joaofranciscosantos opened this issue 1 year ago • 6 comments

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: Screenshot 2023-03-29 at 00 24 13

joaofranciscosantos avatar Mar 28 '23 22:03 joaofranciscosantos

好像不能用import都要用require

DokiDoki1103 avatar Sep 08 '23 17:09 DokiDoki1103

Note that the file is auto-generated. It should not be modified manually

joaofranciscosantos avatar Sep 10 '23 19:09 joaofranciscosantos

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.

atjn avatar Mar 15 '24 12:03 atjn

This should be reopened as the fix was reverted. This needs to be adressed somehow as esm won't go anywhere :)

meros avatar Apr 03 '24 19:04 meros

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.

meros avatar Apr 03 '24 19:04 meros