How to track down an unexpected token in JSON? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
undefined:13
]
^
SyntaxError: Unexpected token ] in JSON at position 267
I have found many similar questions and I assume the problem is that JSON.parse returns incorrect data with an extra ] symbol. Probably HTML instead of JSON or something. But the problem is that I can't figure out how to track down where exactly the problem comes from? I have multiple js files with many JSON.parse included. The answer should be in my error message but I can't figure out where to look at. Line 267 is definitely a wrong answer because it is an empty line in all my files. Thanks.
Full error:
undefined:13
]
^
SyntaxError: Unexpected token ] in JSON at position 267
at JSON.parse (<anonymous>)
at mybp (eval at <anonymous> (/Users/apple/Desktop/UTESTTWO/app/bp.js:39:1), <anonymous>:109:25)
at getaccountdata (/Users/apple/Desktop/UTESTTWO/app/bp.js:64:1)
at Object.<anonymous> (/Users/apple/Desktop/UTESTTWO/app/bp.js:351: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 Object.<anonymous> (/Users/apple/Desktop/UTESTTWO/automatic.js:1:63)
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)

One option is a bit like playing 21 questions. Remove or comment out calls to half of your code and see if the error persists. That will help you tell which half contains the error. Once you know which half it is, then split the code in that half again. Repeat the process and keep reducing your code in half until you narrow it down to the section that contains the error.
When you don't have much else to go on you sometimes must resort to reduction techniques like this to narrow your scope.

Related

Node cannot compile static initializer block [duplicate]

I've been looking at this error for a long time and I just cannot wrap my head around it.
Here's the code:
class Instance {
static {
}
}
module.exports = Instance;
That is all there is. The class was full of stuff but I deleted them all because they are irrevelant. It's giving the same error with or without them.
The error:
static {
^
SyntaxError: Unexpected token '{'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object.<anonymous> (path\to\project\index.js:1:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
I've used static constructors in the past and testing the same code somewhere else seems to work so I dont understand what the issue is here. Please help me.
SOLUTION: Updated Node to 16.13.1 and it now works. 16.7.0 wasnt enough.
It is supported by Node 17.3.0. It is an ES2022 feature. This https://node.green/#ES2022 is a nice link to find ES feature compatibility in Nodejs. You will have to download the current version of Nodejs which is 17.3.0.

Grave (`) is marked as illegal token

I've been trying to transfer a node program I created locally to an EC2 server via Cloud9, and I have already manually replaced all of the (req,res) => {} statements because it didn't accept that. However, now it's giving me
console.log(`foo ${bar}`)
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:945:3
It works fine on my local server and it runs fine here too, even without nodejs.
var bar = "bar"
console.log(`foo ${bar}`)
The version of node is v13.9.0 and it was installed via nvp.
Anyone know why this is happening? I really don't want to go through and convert all the strings with `
Thanks for the help, I figured out that cloud9 automatically installs nodejs, just not the right version. It installs 10.19.0, while the current latest version is 13.10.0. I installed the latest version, but the default was still 10.19.0. For anyone else having this problem, do
nvm ls
to list your installed node versions, then use
nvm alias default 13.10.0 (replace with whatever version you want)

How to Hide Internal Stack Trace Error in Node.js By Default?

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.

"SyntaxError: Unexpected Token" when running

A Photo Of The Code
I do not understand why it is not running, I keep receiving this error
});
^
SyntaxError: Unexpected token }
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:193:16)
Even if you didn't respect the "don't post images" rule, I'll try giving you an answer.
I see in the image that at line 30 you have an else that is not closed, and the next character is a }, so it may be that causing the problem. You should write else {} in order to make the code run.
To avoid this kind of silly errors, try using something like ESLint: I see you're using Atom Editor, there's an atom package for that (link)

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.

Categories