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.
Related
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
I only want to display the TypeError line below, but hide all the internal details (the following 7 lines all starting with at).
~/Desktop/Practice> node "c:\Users\User\Practice\PracJS.js"
c:\Users\User\Practice\PracJS.js:6
let b = duck.prototype.isPrototypeOf(Bird)
^
TypeError: Cannot read property 'isPrototypeOf' of undefined
at Object.<anonymous> (c:\Users\User\Practice\PracJS.js:6:24)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
I know I can do this with Error.stackTraceLimit = 0 by including it in the file. But I want to do this by default.
Is there any way to point Node a config file (setting Error.stackTraceLimit = 0 inside) so that whenever I run node PracJS.js (inside VSCode) so that it automatically applies Error.stackTraceLimit = 0?
Any help is appreciated! Thank you!
You could use --stack-trace-limit flag while running the javascript file. so in your case
node --stack-trace-limit=0 "c:\Users\User\Practice\PracJS.js"
OR
export NODE_OPTIONS='--stack-trace-limit=0'
It will be also worth mentioning to use NODE_ENV for these kind of cases. Some of the good packages (Express etc) uses this variable to control these things. so basically set NODE_ENV=production and you could also use this to control stack trace in your code.
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 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.
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.