Skip to content

protobufjs library itself doesn't provide module support #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #161
gertcuykens opened this issue May 27, 2019 · 5 comments
Open
Tracked by #161

protobufjs library itself doesn't provide module support #1230

gertcuykens opened this issue May 27, 2019 · 5 comments

Comments

@gertcuykens
Copy link

gertcuykens commented May 27, 2019

protobuf.js version: 6.8.8
example: https://github.com/gertcuykens/protobuf

Generated modules looks ok using

pbjs -p . task.proto -t static-module -w es6 -o task.pb.mjs
import protobuf from 'protobufjs'
import {Task} from './task/task.pb.mjs'

But it seems the protobufjs library itself doesn't provide module support?

node --experimental-modules task_test.mjs 
(node:1831) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:69
  let url = moduleWrapResolve(specifier, parentURL);
            ^

Error: Cannot find module '/Users/gert/go/src/github.com/gertcuykens/protobuf/node_modules/protobufjs/minimal' imported from /Users/gert/go/src/github.com/gertcuykens/protobuf/task/task.pb.mjs
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:69:13)
    at Loader.resolve (internal/modules/esm/loader.js:70:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
    at link (internal/modules/esm/module_job.js:42:36)
gert@gert ~/go/src/github.com/gertcuykens/protobuf:master>

Suggest to make a seperate protobufjs/minimal.mjs and protobufjs/index.mjs maybe to have full module support out of the box?

@seishun
Copy link
Contributor

seishun commented Feb 17, 2020

This would be particularly useful to support deno. I would love to help with this, but since protobuf.js uses CommonJS, a design decision needs to be made on how to approach this.

@hsjoberg
Copy link

hsjoberg commented Sep 9, 2020

Any more updates/info on this one?

Using ES6 modules in Node via "type" : "module" in package.json in the root project does not seem to work with protobufjs.

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '[project]/node_modules/protobufjs/minimal' imported from [project]/dist/proto.js
    at finalizeResolution (internal/modules/esm/resolve.js:275:11)
    at moduleResolve (internal/modules/esm/resolve.js:719:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:809:11)
    at Loader.resolve (internal/modules/esm/loader.js:85:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
    at link (internal/modules/esm/module_job.js:50:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

@paweld2
Copy link

paweld2 commented Sep 10, 2020

Trying to import as ESM module to vite project also fails.

I am adding protobuf globally to the page to make it work, but ESM support could be nice.

benjamind added a commit to benjamind/protobuf.js that referenced this issue Jun 10, 2021
benjamind added a commit to benjamind/protobuf.js that referenced this issue Jun 10, 2021
achingbrain added a commit to ipfs/js-ipfs-unixfs that referenced this issue Aug 19, 2021
Switches the codebase to ESM and exports ESM and CJS code. The main goal is to enable ipfs-unixfs to be built on next generation bundlers like skypack: codepen.io/vascosantos/pen/NWjaYqP?editors=0011 This is a problem that has not been fixed in skypack yet. More details: skypackjs/skypack-cdn#171

Given we will need to export ESM and we will, let's get this to circumvent the issue.

We also need to manually change the generated protobuf code due to protobufjs/protobuf.js#1230

BREAKING CHANGE: ./src/dir-sharded is not in the exports map so cannot be imported

Co-authored-by: Alex Potsides <[email protected]>
@antonio-ivanovski
Copy link

Seems to have been resolved, no longer getting the error ⭐ 🚀

@Its-Ankush
Copy link

I have my HTML like this

    <script src="//cdn.jsdelivr.net/npm/[email protected]/dist/protobuf.min.js"></script>
    <script src="./proto/proto.js" type="module"></script>

I have compiled my proto

syntax="proto3";

// message = type of object
// ClientMessage =  name of object
message ClientMessage {
    string message =1;
}


message ServerMessage {
    string username =1;
    string message =2;
    bool history=3;
}

into js using this command

pbjs -t static-module  -w es6 -o proto.js chat.proto

Im not use node or anything. Just want my compiled file to be able to use the proto protobuf.min.js from the cdn. No matter what i do i get this error

Uncaught TypeError: Failed to resolve module specifier "protobufjs/minimal". Relative references must start with either "/", "./", or "../".

All due to this line in proto.js [ the compiled js ]

import * as $protobuf from "protobufjs/minimal";

Now im out of options. Can anyone please help ? As if the compiled code is unable to see the min.js pulled by the CDN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants