Nodejs - Crypto error - this._handle.update(data, encoding); - javascript

I'm using node v4.8.0 (npm v3.10.10) and in my service code i'm generating a hash using Crypto something like below,
hash = crypto.createHash('sha256')
.update(val1+val2+JSON.stringify(val3), 'utf8')
.digest('hex');
when i build the app it fails with the below error, it was working few weeks back and all of a sudden ended getting the error. Any idea how could i overcome this one, this seems to be a viable solution https://github.com/webpack/webpack/issues/4072 but not getting it fully though since i'm not explicitly using web pack as i'm dealing with an API and not a client app
Error:
crypto.js:70
this._handle.update(data, encoding);
^
TypeError: Not a string or buffer
at TypeError (native)
at Hash.update (crypto.js:70:16)
at Object.<anonymous> (/srv/approot/node_modules/v8flags/index.js:14:81)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
......

It works for me.
So I guess one of your data is null or something else than a string or buffer.

Related

Command line "node index.js" does not output correctly on windows 10, how to make it work?

SOLUTION: I found the solution to my problem, it was wrongly encoded. I created the file in the terminal (just to test around what the terminal can do), and it encoded it wrongly. I just created a new file and tried out console.log("Hello"); and that worked out GREAT in the terminal.
PROBLEM:
I've installed Node.js yesterday while following a tutorial on it, and when I tried to run the command node index.js I get the following inside my terminal:
$ node index.js
C:\Users\NAME\Desktop\intro-to-node\index.js:1
��c
SyntaxError: Invalid or unexpected token
?[90m at wrapSafe (internal/modules/cjs/loader.js:1053:16)?[39m
?[90m at Module._compile (internal/modules/cjs/loader.js:1101:27)?[39m
?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:1
0)?[39m
?[90m at Module.load (internal/modules/cjs/loader.js:985:32)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)?[39m
?[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_ma
in.js:71:12)?[39m
?[90m at internal/main/run_main_module.js:17:47?[39m
I've also followed this guide here: https://nodejs.org/download/release/v12.18.3/docs/api/synopsis.html I copied the content of the code into my file as well, but regardless I get the same output in my terminal.
At first I had console.log("Hello World"); and then I tried it with:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I am using Git Bash as a Terminal, but the same happened with MS PowerShell and CMD. I am on Windows 10, the latest update as well. Node Version is v12.18.3. It's also inside the PATH, node -v works fine and I get the version number as well, so does node -i.
I think you really have some special characters in the file.
Please copy the exact character �� and find in the editor you are using.
SOLUTION: I found the solution to my problem, it was wrongly encoded. I created the file in the terminal (just to test around what the terminal can do), and it encoded it wrongly. I just created a new file and tried out console.log("Hello"); and that worked out GREAT in the terminal.
You are trying to create a server using http.createServer, but it's not imported into the file. Please require it as below.
const http = require("http");
This will solve your issue

Apollo-server tutorial from GraphQL website produce error

I can't get this tutorial to work (running on Ubuntu v19.04):
https://www.apollographql.com/docs/apollo-server/getting-started/
My code is literally copy+pasted from the tutorial - I have installed all dependencies, and I keep getting the following error when running node index.js:
internal/modules/cjs/loader.js:628
throw e;
^
Error: No valid exports main found for '{project-path}/node_modules/uuid'
at resolveExportsTarget (internal/modules/cjs/loader.js:625:9)
at applyExports (internal/modules/cjs/loader.js:502:14)
at resolveExports (internal/modules/cjs/loader.js:551:12)
at Function.Module._findPath (internal/modules/cjs/loader.js:657:22)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:960:27)
at Function.Module._load (internal/modules/cjs/loader.js:855:27)
at Module.require (internal/modules/cjs/loader.js:1033:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> ({project-path}/project-myra/node_modules/apollo-engine-reporting/dist/agent.js:26:16)
at Module._compile (internal/modules/cjs/loader.js:1144:30) {
code: 'MODULE_NOT_FOUND'
This is literally so frustrating, because the only place to get any help with apollo-server is from a Spectrum chat that keeps disconnecting, and doesn't even allow me to create any posts...
Why is this library the official supported and recommended library for GraphQL? And how come I seem to be the only person in the world having this issue, I can't find anyone else asking about this...
It seems it's a problem with the current Apollo Server version.
Try downgrading to 2.6.3, this worked for me. Haven't tried with any other version though.
npm install apollo-server#2.6.3

Syntax error at new Script (vm.js:51:7) whilst running code for a discord bot in javascript

I get an error whilst running my node index.js
here is my code
const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const bot = new Discord.Client({disableEveryone: true});
bot.on("ready", async () => {
console.log(`${bot.user.username} is online!`);
}};
bot.login(botconfig.token);
here is my error message
SyntaxError: missing ) after argument list
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:190:16)
hope you can help!
If you see an error at new Script (vm.js:51:7), it means there's an error in a custom script that you passed to vm.js, the Node module that communicates with the V8 Virtual Machine.
new Script in vm.js is simply evaluating your code.
So you need to work out what the fault is in the code you passed to the V8 virtual machine. If you run the file directly (e.g. node some/path/some_file.js) you should get a pointer to the actual fault that looks like this:
YourPC:your-directory you$ node some/path/some_file.js
/some/system/path/your-directory./some/path/some_file.js:123
}};
^
SyntaxError: missing ) after argument list
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
The part above the error message with the ^ caret shows you the faulty point in your own code.
In your case, it's pretty easy to spot: you have a }}; that should be a });.
If you have code that seems 100% fine but encounters this error, like #maevanapcontact's failing arrow functions, maybe you're using an old version of Node with an old version of V8 that didn't support that ECMAScript feature. Arrow functions didn't have complete support until Node version 6.
I had the same error as you and I have fixed it by using function(){}; instead of () =>.
I don't really know why it doesn't work with arrow functions, but it did the job for me like that.
I found it helpful to add a break-point in vm.js where the error is thrown. (click on the filename link in the stack trace).
Reload the page and then inspect the local variables.
The filename variable will give the the full name of the js file that caused the error.
Unfortunately this isn't enough to narrow down the exact line, so I ended up deleting parts of the file until it would compile. From there I was able to narrow what was causing the error. In my case, my IDE linter wasn't giving me any hints either.
The following helped me.
Delete all node_modules
rm -rf node_modules/
and then install
npm install

I get a SyntaxError in Node.JS, but I can't figure out the origin of that error from the log messages

I am running a Node app on Azure. It's a Web API and when I try to call it, I see this message in the Node console:
Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token (
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:17:23)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
I ran my whole code through eslint and didn't find any syntax errors. It also runs fine on my local machine, but doesn't run on Azure.
The message above points to line 17 and column 23 in app.js, which is a require:
let warehouseExport = require('./routes/warehouse-export-api');
I ran eslint and jshint specifically on the file referenced above, and there are no syntax errors.
What are some other ways I can get the exact location of the syntax error, and why could it be that the code fails on Azure but runs on my local machine?
What I tried:
Wrap in try-catch and grab the line number - it shows up undefined:
try {
var warehouseExport = require('./routes/warehouse-export-api');
} catch (error) {
console.log(error + ', line ' + error.lineNumber);
}
You should check version of nodejs you have installed on Azure. Maybe it's <= 4 and it doesn't support let keyword. Just a wild guess.
The cause of the error was that the Node version somehow got reset to 4.2.4.
When I go to Azure Portal and run node --version, I get version 8.1.4.
However, if I console.log the Node version inside my script, it is somehow still 4.2.4.
I will investigate why that happened and how to fix it, but for now it's clear that the bug is due to async function not being recognized by the old Node.
Update: the package.json file where the node version was specified got overwritten somehow, which is what caused the issue.

learnyounode: #3 My First I/O

I am trying this in Visual Studio Code on Windows 10 with powershell.exe as Terminal.
After many failures, I peaked in the internet to find its solution.
Here's the solution that I have got from the internet:
var fs = require('fs');
      
var contents = fs.readFileSync(process.argv[2]);
var lines = contents.toString().split('\n').length - 1;
console.log(lines);
I saved it in a file, myFirstIO.js. However, I tried running it on PowerShell, I received the following error:
PS C:\Users\aps12\Desktop\Test\lyn> node myFirstIO.js
fs.js:640
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
TypeError: path must be a string or Buffer
at TypeError (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
at Object.<anonymous> (C:\Users\aps12\Desktop\Test\lyn\myFirstIO.js:3:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
But when typed,
PS C:\Users\aps12\Desktop\Test\lyn> learnyounode verify myFirstIO.js
Surprisingly, solution got passed.
# PASS Your solution to MY FIRST I/O! passed!
I wonder why the solution get passed even when it was not running.
Also, why the solution was not running? What is the mistake I have made there?
PS: I have already cleared first two modules of learnyounode without any failure.
If you run this program using node myFirstIO.js it will throw an error, because it expects a file as second argument
/*
on this line, file being read using readFileSync and saving it to a
contents variable
*/
var contents = fs.readFileSync(process.argv[2]);
If you notice the error TypeError: path must be a string or Buffer, it expecting a file path string or file stream as second argument.
When you are running this program with learnyounode, they implicitly running it with a file, you can also test this by running it like node myFirstIO.js textfile.txt
Note, file path must be valid.
If you run this program like node myFirstIO.js "Hello World" it won't work, as it expects a file path or stream not a string. Hope this helps you understand this issue.
process.argv returns all arguments given in command line.
When you do > node myFirstIO.js, you're passing these arguments to process.argv:
['node', 'myFirstIO.js']
... It threw an error cuz you were asking for the third argument (process.argv[2]), which you didn't pass in the command line.
Read about process.argv here.
Now, for learnyounode, either I assume you passed the third argument when you used:
> learnyounode verify myFirstIO.js
using the JS file as third argument; or learnyounode is buggy.

Categories