Reputation: 250
I'm trying to get protobuf to run in typescript.
The official Google Documentation says to simply use npm install google-protobuf
and then to require('google-protobuf')
.
I'm unexperienced with npm, so I ran into several problems there. First of all, require('google-protobuf')
returned an error 404, because the file was not found. I instead opted to require the file manually, so I sourced it in my index.html
:
<script src="node_modules/google-protobuf/google-protobuf.js"></script>
This should work right?
What instead happens is that I get an Uncaught reference error: exports is not defined
. How do I even begin to debug this? I tried to take a look at the google-protobuf.js file and found a few exports
statements, but I don't know what I'm expected to do here.
If it helps at all, here is my tsconfig.json
file:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
Upvotes: 3
Views: 2589
Reputation: 8497
First, please make sure you have a proper environment. I suggest you to:
Then, try the following solutions:
// test.js
var protobuf = require('google-protobuf');
console.log(protobuf);
Run the script with node test.js
.
// test.ts
import * as protobuf from 'google-protobuf';
console.log(protobuf);
Run the script with ts-node test.ts
.
If you see a regular object in your terminal, congrats! The module is working. But now, if you want to use it in your browser, you will need module loaders/bundlers like Browserify or Webpack...
Upvotes: 1
Reputation: 2920
you are using typescript. Try
import protobuf = reqquire('google-protobuf')
or install the types via npm install @types/google-protobuf'
and include it with import * as protobuf from 'google-protobuf'
The include in the HTML does not work, because the js files use imports and the dependencies are not resolved.
If you want to use this on the client side, you can finally build your fronted js file with browserify.
Upvotes: 0