protoc generate typescript error: Must use import to load ES Module

57 views Asked by At

bash script:

#!/bin/bash
PROTOC_GEN_PLUGEN_PATH="/d/Program Files/nodejs/protoc-gen-ts_proto.cmd"
protoc --plugin=protoc-gen-ts_proto="$PROTOC_GEN_PLUGEN_PATH" --ts_proto_out=../pb ./*.proto

error:

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\node_modules\case-anything\dist\index.js
require() of ES modules is not supported.
require() of C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\node_modules\case-anything\dist\index.js from C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\build\case.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\node_modules\case-anything\package.json.

    at Object.Module._extensions..js (internal/modules    s/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:100    02:32)
    at Function.Module._load (internal/modules/cjs/loa    ader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:    :1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)    )
    at Object.<anonymous> (C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\build\case.js:4:2        25)
    at Module._compile (internal/modules/cjs/loader.js    s:1158:30)
    at Object.Module._extensions..js (internal/modules    s/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:100    02:32)
    at Function.Module._load (internal/modules/cjs/loa    ader.js:901:14) {
  code: 'ERR_REQUIRE_ESM'
}
--ts_proto_out: protoc-gen-ts_proto: Plugin failed with status code 1.
FAILED:Failed to generate TypeScript code!!!
Now using node v12.16.1 (64-bit)

yjby@һ□□□□□□ MINGW64 /d/project/CCB-dev-3.0/ccb-server/webServer/schedule
$ bash ./protoc-gen-ts.sh 
Now using node v12.16.1 (64-bit)
internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\node_modules\case-anything\dist\index.js
require() of ES modules is not supported.
require() of C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\node_modules\case-anything\dist\index.js from C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\build\case.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\node_modules\case-anything\package.json.

    at Object.Module._extensions..js (internal/modules    s/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:100    02:32)
    at Function.Module._load (internal/modules/cjs/loa    ader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:    :1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)    )
    at Object.<anonymous> (C:\Users\yjby\AppData\Roaming\nvm\v12.16.1\node_modules\ts-proto\build\case.js:4:2        25)
    at Module._compile (internal/modules/cjs/loader.js    s:1158:30)
    at Object.Module._extensions..js (internal/modules    s/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:100    02:32)
    at Function.Module._load (internal/modules/cjs/loa    ader.js:901:14) {
  code: 'ERR_REQUIRE_ESM'
}
--ts_proto_out: protoc-gen-ts_proto: Plugin failed with status code 1.
FAILED:Failed to generate TypeScript code!!!

environment: node version: v12.16.1 ts-node: "^8.6.2" protoc version: libprotoc 3.13.0 tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "esModuleInterop": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

what should i do?

version 14.18.3 seems to work, but i can't switch node version. who can help me, thanks.

0

There are 0 answers