Skip to content

Cannot find module "protobuf/minimal", "Did you mean to import protobufjs/minimal.js?" #1862

@birgersp

Description

@birgersp

protobuf.js version: 7.2.0

Trying to use protobufjs in an ES module.

{
	"name": "myproject",
	"version": "1.0.0",
	"type": "module",
	"dependencies": {
		"protobufjs": "^7.2.0"
	},
	"devDependencies": {
		"protobufjs-cli": "^1.1.0",
		"typescript": "^4.9.5"
	}
}

I generated the protobuf messages module (and typings) like this

npx pbjs -t static-module -w es6 protobuf/myproject.proto -o src/protobuf/protobuf.js
npx pbts src/protobuf/protobuf.js -o src/protobuf/protobuf.d.ts

At the top of the src/protobuf/protobuf.js file I see this:

/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
import * as $protobuf from "protobufjs/minimal";

Given a simple program

// src/main.ts
import { SomeMessage } from "src/protobuf.js"
console.log("Hello world!")

And compiling with this tsconfig.json:

{
	"compilerOptions": {
		"module": "NodeNext",
		"moduleResolution": "nodenext",
		"target": "ES2022",
		"sourceMap": true,
		"strict": true,
		"skipLibCheck": true,
		"outDir": "build",
		"noImplicitAny": false,
		"allowJs": true
	},
	"include": [
		"src/**/*",
		"tests/**/*"
	]
}

When I try to run the project node --enable-source-maps build/main.js I get this error message:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/birger/myproject/node_modules/protobufjs/minimal' imported from /home/birger/myproject/build/src/protobuf/protobuf.js
Did you mean to import protobufjs/minimal.js?

Tbh I don't know if I correctly understood the process of generating the protobuf module with typings. Should I not be using the -w es6 flag?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions