Proto files using google.protobuf imports - causing issues in NodeJS - node.js

I have google.profobuf.* imports in a proto file (used by GRPC server written in go). When the same proto file is used to implement a GRPC client in NodeJS - I run into issues.
Details:
proto file used by the GRPC server (written in go):
tech.proto
syntax = "proto3";
package api;
import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";
message Info {
string desc = 1;
google.protobuf.Duration ttl = 2;
}
service Tech {
rpc BasicInfo(google.protobuf.Empty) returns (Info) {}
}
When this is used by the GRPC client written in NodeJs:
getTechInfo.js (first few lines)
'use strict';
const PROTO_PATH = __dirname + '/../../api/tech.proto';
const grpc = require('grpc');
const apiProto = grpc.load(PROTO_PATH).api;
I get the following error:
/Users/././node_modules/protobufjs/dist/protobuf.js:4720
throw Error("failed to import '"+importFilename+"' in '"+filename+"': file not found");
^
Error: failed to import '/Users/././api/google/protobuf/duration.proto' in '/Users/././api/register.proto': file not found
at Builder.ProtoBuf.Builder.BuilderPrototype.import (/Users/././node_modules/protobufjs/dist/protobuf.js:4720:35)
at Object.ProtoBuf.loadJson (/Users/././node_modules/protobufjs/dist/protobuf.js:5225:26)
at Object.ProtoBuf.loadProto (/Users/././node_modules/protobufjs/dist/protobuf.js:5128:25)
at Object.ProtoBuf.loadProtoFile (/Users/././node_modules/protobufjs/dist/protobuf.js:5174:52)
at Object.load (/Users/././node_modules/grpc/index.js:135:26)
at Object.<anonymous> (/Users/././src/api/getTechInfo.js:5:23)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
The issue is with the imports in the proto file:
import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";
What is the recommended method to resolve these imports? Thanks in advance.
Using:
Node v8.9.4
"google-protobuf": "^3.5.0",
"grpc": "^1.10.1",

This is a known issue with the gRPC library, documented primarily in this issue. The grpc.load API does not support easily loading google/protobuf/*.proto files.
The simplest solution is to use the #grpc/proto-loader library, which automatically includes google/protobuf/*.proto files when loading proto files.
One alternative solution is to use grpc-tools to pre-generate files that can be used with the google-protobuf library.

Related

Error "The specified module could not be found" on hyperledger Aries

after doing everything from https://aries.js.org/guides/getting-started/set-up , i'm trying to copy paste this code to initialize the agent
//imports for indy-sdk
import type { InitConfig } from '#aries-framework/core'
import { Agent } from '#aries-framework/core'
import { agentDependencies } from '#aries-framework/node'
import { HttpOutboundTransport, WsOutboundTransport } from '#aries-framework/core'
import { HttpInboundTransport } from '#aries-framework/node'
// The agent initialization configuration
const config: InitConfig = {
label: 'docs-nodejs-agent',
walletConfig: {
id: 'wallet-id',
key: 'testkey0000000000000000000000000',
},
}
// Creating an agent instance
const agent = new Agent(config, agentDependencies)
// Registering the required in- and outbound transports
agent.registerOutboundTransport(new HttpOutboundTransport())
agent.registerInboundTransport(new HttpInboundTransport({ port: 3000 }))
// Function to initialize the agent
const initialize = async () => await agent.initialize().catch(console.error)
but on visual studio 2022, it gives me this error:
C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\bindings\bindings.js:121
throw e;
^
Error: The specified module could not be found.
\\?\C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\indy-sdk\build\Release\indynodejs.node
at Module._extensions..node (node:internal/modules/cjs/loader:1243:18)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at bindings (C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\bindings\bindings.js:112:48)
at Object.<anonymous> (C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\indy-sdk\src\indyBinding.js:1:37)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32) {
code: 'ERR_DLOPEN_FAILED'
}
Node.js v19.0.1
the problem is that the file at C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\indy-sdk\build\Release\indynodejs.node exists and is right....
Thanks in advance!
I had the same problem and i think i found the solution at least for windows so i hope you're also on windows otherwise i don't think this will help you a lot.
First step is: downloading the the prebuilt libraries of the indy-sdk at https://repo.sovrin.org/windows/libindy/master/1.16.0-1636/libindy_1.16.0.zip as stated in https://aries.js.org/guides/getting-started/installation/nodejs/windows
Second step is: unpacking these somewhere in a folder
Third step is: Setting these libraries in your Environment variables > System Variables as stated in the docs linked above as LD_LIBRARY_PATH and make sure the value points to the /lib folder of where you unpacked the prebuilt libraries
I did these steps above and still had the issue you had and somewhere i found a mention that the indy-sdk requires the libindy in your system PATH https://www.npmjs.com/package/indy-sdk. So i did exactly that as well.
So the fourth step is: Select Path under your System variables and click Edit. Then click new and paste the location of your extracted /lib so the same value that the LD_LIBRARY_PATH has.
Fifth Step is not forgetting to restart and retreading the installing of dependencies with yarn add ... as stated in the setup https://aries.js.org/guides/getting-started/set-up. This can be crucial since it took a restart for it to work after doing all of this
Let me know if this worked for you !

Error in constructing Graphql schema and resolvers in deployment of Next.js app on Vercel

I am using graphql-tools to merge Types and Schemas. My graphql server runs on the end point api/graphql
import { mergeTypeDefs, mergeResolvers } from "#graphql-tools/merge";
import { loadFilesSync } from "#graphql-tools/load-files";
import path from "path";
const typesArray = loadFilesSync(path.join(process.cwd(), "./**/*.graphql"));
export const types = mergeTypeDefs(typesArray);
const resolversArray = loadFilesSync(
path.join(process.cwd(), "./**/*.resolvers.*")
);
export const resolvers = mergeResolvers(resolversArray);
The graphql endpoint works locally, however upon deployment to Vercel, I get the following error with Serverless Function when an API call is made to api/graphql
ERROR Unhandled error during request: Error: Query root type must be provided.
at assertValidSchema (/var/task/node_modules/graphql/type/validate.js:69:11)
at assertValidExecutionArguments (/var/task/node_modules/graphql/execution/execute.js:150:35)
at executeImpl (/var/task/node_modules/graphql/execution/execute.js:98:3)
at Object.execute (/var/task/node_modules/graphql/execution/execute.js:60:63)
at Object.generateSchemaHash (/var/task/node_modules/apollo-server-core/dist/utils/schemaHash.js:15:32)
at ApolloServer.generateSchemaDerivedData (/var/task/node_modules/apollo-server-core/dist/ApolloServer.js:269:41)
at new ApolloServerBase (/var/task/node_modules/apollo-server-core/dist/ApolloServer.js:161:38)
at new ApolloServer (/var/task/node_modules/apollo-server-micro/dist/ApolloServer.js:18:1)
at Module.dCYn (/var/task/.next/serverless/pages/api/graphql.js:490:22)
at __webpack_require__ (/var/task/.next/serverless/pages/api/graphql.js:23:31)
at Module.GfcW (/var/task/.next/serverless/pages/api/graphql.js:251:23)
at __webpack_require__ (/var/task/.next/serverless/pages/api/graphql.js:23:31)
at /var/task/.next/serverless/pages/api/graphql.js:91:18
at Object.<anonymous> (/var/task/.next/serverless/pages/api/graphql.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
I believe it has to do with a mess up in the path for loadFilesSync. I tried replacing process.cwd() with __dirname, but it didn't work. I pretty much have scoured through every single resource on the internet but no luck. Any help would be greatly appreciated.

Error while importing one file into another in Node.JS

I am using import while importing some functions from my practice.js file into different.js file.
practice.js file:-
function sum(x,y){
return x+y;
}
const pi = 3.14;
module.exports = {
sum : sum,
pi:pi
};
different.js file:-
import {sum,pi} from "./practice.js";
console.log("2 pie: "+sum(pi,pi));
Now when I am using require, the output is proper and no error is given.
When I am using import, there is this following error:-
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:749:23)
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:816:10)
at Module.load (internal/modules/cjs/loader.js:672:32)
at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
at Function.Module._load (internal/modules/cjs/loader.js:604:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:868:12)
at internal/main/run_main_module.js:21:11
I have asked my colleagues and they told me that this is about ES6 and Babel is not configured in your system.
But I am not sure how to proceed with this. Can anybody please help me how to do it?
Rename your main file (different.js) to different.mjs.
Rename your practice.js file to practice.mjs and make it look like this:
function sum(x, y) {
return x + y;
}
const pi = 3.14;
export {sum, pi};
Then run node --experimental-modules different.mjs to run Node with it's experimental module loader.
You can read more here

Knex: How to fix "Cannot read property 'prototype' of undefined" on ARM for initial-setup

I am trying to initialize a sqlite3 database with knex on an ARM-Device, but getting the error:
Knex: run
$ npm install sqlite3 --save
TypeError: Cannot read property 'prototype' of undefined
at inherits (/home/user/node_modules/sqlite3/lib/sqlite3.js:27:16)
at Object.<anonymous> (/home/user/node_modules/sqlite3/lib/sqlite3.js:66:1)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Client_SQLite3._driver (/home/user/sWave-Gateway/node_modules/knex/lib/dialects/sqlite3/index.js:79:12)
at Client_SQLite3.initializeDriver (/home/user/sWave-Gateway/node_modules/knex/lib/client.js:254:26)
at Client_SQLite3.Client (/home/user/sWave-Gateway/node_modules/knex/lib/client.js:115:10)
at new Client_SQLite3 (/home/user/sWave-Gateway/node_modules/knex/lib/dialects/sqlite3/index.js:62:20)
at Knex (/home/user/node_modules/knex/lib/index.js:60:34)
at Object.<anonymous> (/home/user/dist/db/knex-data-access-layer/index.js:28:28)
at Module._compile (module.js:653:30)
I already tried to set the NODE_ENV in different ways set the rights of the files with chmod to 777 but nothing worked so far. I am kind of despaired because i have not changed anything on this part for a long time and it suddenly stopped working.
The Command i use:
NODE_ENV=production node dist/initial-setup.js
It executes the following code:
import * as config from 'config';
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
import { boot } from './boot';
import * as constants from './constants';
import { dataAccessLayer } from './db';
import * as shell from 'shelljs';
// tslint:disable:no-console
boot();
let logPath: string = config.get(constants.CONFIG_LOG_DIR);
if (!fs.existsSync(logPath)) {
console.log(`Creating logs directory at ${logPath} ...`);
mkdirp.sync(logPath);
}
let secretDirPath: string = config.get(constants.CONFIG_SECRET_DIR);
if (!fs.existsSync(secretDirPath)) {
console.log(`Creating secret directory at ${secretDirPath} ...`);
mkdirp.sync(secretDirPath);
}
let jwtSecret: string = crypto.randomBytes(config.get(constants.CONFIG_JWT_RANDOM_BYTES)).toString('hex');
let jwtSecretPath: string = path.join(secretDirPath, config.get(constants.CONFIG_JWT_SECRET_FILE));
fs.writeFileSync(jwtSecretPath, jwtSecret, 'utf8');
async function setupDb(): Promise<void> {
await dataAccessLayer.migrate();
try {
await dataAccessLayer.seed();
} catch (e) {
// ignore missing production seeds, rethrow otherwise
if (e.toString().indexOf('volatile-seeds/production') === -1) {
throw e;
}
}
}
setupDb().catch(e => console.log(e))
.then(()=> {
shell.exec('tskill node');
});
The problem was that the newest sqlite3 4.0.8 version will not work correctly on this ARM-processor. I downgraded it to 4.0.6 and now it works flawless.
I also had this problem when upgrading from sqlite3 version 4.0.4 to version 4.1.0. Pinning my dependency to 4.0.4 got it working again. You can also see some other workarounds here and here, and discussion of usage in browser environments here.

'use-strict' enabled but not working in node

I have enabled use-strict mode in my .js file but when I run it, node keeps telling me that I don't have it enabled. PLEASE don't tell me to write "use-strict"; at the top of my file because I already tried that.
Here is my server.js file. I have been trying to see what is wrong but so far stack overflow has not been much help since most people seem to get this working on their first try.
require('use-strict')
'use-strict';
let util = require('util');
let http = require('http');
let Bot = require('#kikinteractive/kik');
var kik_username = process.env.KIK_USERNAME;
var kik_api_key = process.env.KIK_API_KEY;
var kik_baseUrl = process.env.KIK_BASEURL;
// Configure the bot API endpoint, details for your bot
let bot = new Bot({
username: kik_username,
apiKey: kik_api_key,
baseUrl: kik_baseUrl
});
bot.updateBotConfiguration();
bot.onTextMessage((message) => {
message.reply(message.body);
});
// Set up your server and start listening
let server = http.createServer(bot.incoming()).listen(8085);
Everything seems fine but when I run
$ node server.js
I keep getting this error
let util = require('util');
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
at node.js:405:3
It tells me to enable strict mode BUT I ALREADY DID THAT. I even required an npm package to make sure I was doing it right! Can anyone make sense of what is happening?
No dash in 'use strict'
'use strict' // not 'use-strict'
Check out the documentation for further reference
You don't need to require an npm package. just put "use strict"; at the top of the js file.

Resources