I am are currently trying to migrate my microservices to use ES modules. Everything went great until it was time to migrate those that use the @grpc/proto-loader
module as I am unable to generate valid ES module imports.
In the current configuration the proto-loader-gen-types
script is use to transform the .proto
files to the Typescript types as per documentation and is working fine.
The script used is as follows: proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=./src/protobuild/ ./src/protobuild/*.proto
The main issue is that the generated code looks something similar to this:
import type { ExampleClient as _example_package_ExampleClient, ExampleDefinition as _example_package_ExampleDefinition } from './example_package/Example';
When adding "type": "module"
to the package.json
and the relevant config to .tsconfig.json
file imports similar to the following are expected:
import type { ExampleClient as _example_package_ExampleClient, ExampleDefinition as _example_package_ExampleDefinition } from './example_package/Example.js';
Notice the .js
at the end of the import route. In the current state my build step fails with errors like:
Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './example_package/Example.js'?ts(2835)
Relevant .tsconfig.json
settings:
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"declaration": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"skipLibCheck": true
},
Is there any way to modify the proto-loader-gen-types
command to generate valid imports or any other config to be changed?
Note: I do prefer not to include any other tooling/package to the build chain if possible.