Why quotes in attribute name raise an error? - javascript

When I execute a file with node.js containing only {"test":1}, a SyntaxError is raised :
(function (exports, require, module, __filename, __dirname) { {"test":1}
^
SyntaxError: Unexpected token :
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:902:3
But {test:1} (without quotes) or var t = {"test":1} works fine.
I execute the file containing the code running : node test.js.
Why ?

{} forms a block.
test: is a label, which is valid (but pointless since there is no loop).
"test": is a string, followed by a colon, which is nonsense.
var foo = {} puts the {} in a different context, so they form an object literal instead of a block. Inside an object literal, property names can be identifiers or strings.
Your test.js content looks like JSON, not JavaScript. It makes no sense to execute it because it doesn't do anything.

Related

SyntaxError: Identifier 'embed' has already been declared

i need help with this
SyntaxError: Identifier 'embed' has already been declared
When i try to make
if (owner && msg.author.id !== 725959811434414091) return;
than i create a embed with using let embed = new Discord.RichEmbed()
it say
^
SyntaxError: Identifier 'embed' has already been declared
at wrapSafe (internal/modules/cjs/loader.js:1047:16)
at Module._compile (internal/modules/cjs/loader.js:1097:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at C:\Users\serveradmin\Desktop\backup\node_modules\require-all\index.js:52:46
at Array.forEach (<anonymous>)
at requireAll (C:\Users\serveradmin\Desktop\backup\node_modules\require-all\index.js:34:9
You should try to rename your variable embed to something else, as this name seems to already be used higher in your file.
If you can't, or don't want to, simply press Ctrl and F at the same time on your code editor, and search for all occurrences of embed, to find out where the other declaration was made.

How to "require" a JS class that contains JSX?

I'm trying to call functions of my JS class which extends react.
I have one file RunTest.js which looks like this:
let Builder = require('./Builder');
let b = Builder();
b.say("Hello World!");
My Builder.js in the same directory looks something like this:
class Builder extends React.Component{
...
}
module.exports = { Builder }
And when I run with node RunTest.js I receive the following error:
bash-3.2$ node RunTest.js
/Users/jonahlibrach/Documents/project/prototypes/bron/v1/java/prototype/
server/static/protocol/Builder.js:322
<div class="navbar">
^
SyntaxError: Unexpected token <
at Module._compile (internal/modules/cjs/loader.js:743:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:810
:10)
at Module.load (internal/modules/cjs/loader.js:666:32)
at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
at Function.Module._load (internal/modules/cjs/loader.js:598:3)
at Module.require (internal/modules/cjs/loader.js:705:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object.<anonymous> (/Users/jonahlibrach/Documents/project/prototy
pes/bron/v1/java/prototype/server/static/protocol/RunTest.js:1:15)
at Module._compile (internal/modules/cjs/loader.js:799:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:810
:10)
How do I use this class in node?
You cannot use modules that contain JSX in Node.js without first transpiling them to JavaScript (e.g. with Babel).

Typescirpt : XMLHttpRequest is not defined in .ts file

i have a .ts file with this code:
var xhttp = new XMLHttpRequest();
when i run the grunt task to build the ts files with typescript, there are no errors.
When i try to instance the class and call the function with this line appear this error:
ReferenceError: XMLHttpRequest is not defined
at HttpRequester.send (C:**\asset-player\src\assetlib\HttpRequester.js:9:25)
at AssetFinder.Asset.req (C:**\asset-player\src\assetlib\Asset.js:41:21)
at Object. (C:**\asset-player\index.js:21:20)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:429:10)
at startup (node.js:139:18)
at node.js:999:3
You'll have to reference a file which (eventually) contains XMLHttpRequest declaration.
That declaration is eventually here:
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.webworker.d.ts
You may want to double-check your typescript configuration:
https://github.com/Microsoft/TypeScript

Meaning of "TypeError: string is not a function"

What does the error "TypeError: string is not a function" mean exactly?
Which conditions trigger the error?
A little context to show where the error appears.
The following program
"use strict;"
((console["log"])(42));
gives the error
/private/var/folders/k6/grq8nv093hj78x5m172d725m0000gn/T/tmp14388866091438886609658.js:2
((console["log"])(42));
^
TypeError: string is not a function
at Object.<anonymous> (/private/var/folders/k6/grq8nv093hj78x5m172d725m0000gn/T/tmp14388866091438886609658.js:2:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
in Node. But console["log"] aka console.log ought to be a function.
However this program
"use strict;"
function displayln(v){return ((console["log"])(v));};
(displayln(42));
runs without any errors.
I think it thinks you're trying to execute "use strict;" as a function. Try adding a semi colon after it
"use strict";
((console["log"])(42));

Unexpected token ILLEGAL while running node.js mocha test

I have searched all over but have not figured out what I am doing wrong. I am trying to set up mocha for testing node.js javascript application files. I have node installed and have successfully ran basic things on it to confirm it is working.
I installed mocha in my project file, and also have a Makefile and a file called "test" within my project file as well.
Here is the error my terminal(osx 10) is spitting out when I run the command "make test".
humbleMousesMBP:chapter02 humbleMouse$ make test
/Users/humbleMouse/chapter02/test/exchange.test.js:22

^
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:313:27
at Array.forEach (native)
at load (/Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:310:9)
at Object.<anonymous> (/Users/humbleMouse/chapter02/node_modules/mocha /bin/_mocha:301:1)
at Module._compile (module.js:456:26)
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:906:3
make: *** [test] Error 8
This is the test I am trying to run:
'use strict';
var assert = require('assert')
, should = require('should');
var exchangeData = {};
suite('exchange', function() {
test('buy should add a BUY nockmarket order', function(done) {
exchangeData = exchange.buy(40, 100, exchangeData);
exchangeData.buys.volumes[40].should.eql(100);
done();
});
test('sell should add a SELL nockmarket order', function(done) {
exchangeData = exchange.sell(41, 200, exchangeData);
exchangeData.sells.volumes['41'].should.eql(200); //this is line 22
done();
});

test('sell should produce trades', function(done) {
exchangeData = exchange.sell(40, 75, exchangeData);
exchangeData.trades[0].price.should.eql(40);
exchangeData.trades[0].volume.should.eql(75);
exchangeData.buys.volumes[40].should.eql(25);
exchangeData.sells.volumes[41].should.eql(200);
done();
});
});
There are some invalid characters in your code, if you used proper text editor, you'd see them. The line numbering is a bit off, but this is clearly the cause.
Here's a screenshot from Sublime Text:
It's \uFFFC, more info here.
Just delete them (they can't be seen, so delete all from the semicolon to the next test().

Categories