I'm newbie of javascript and I want to open json file in node.js.
What's the problem of this "SyntaxError: Unexpected token"?
$ cat jsfile
{'key1':'val1', 'key2':'val2'}
obj = require("./jsfile");
jsfile:1
(function (exports, require, module, __filename, __dirname) {
{'key1':'val1', 'key2':'val2'}
^
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at repl:1:7
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
Your file isn't valid JSON.
JSON strings should be delimited by double quotes, not single quotes.
So your file should look like this:
{"key1":"val1", "key2":"val2"}
Also, JSON files should have a .json extension:
$ cat jsfile.json
{"key1":"val1", "key2":"val2"}
obj = require('./jsfile.json');
Related
I'm trying to read CSV with Javascript, but I'm getting an error.
Here is my code:
fs.readFile('./data/AMZN.csv', async (err,data)=>{
if(err){
console.log(err)
return
}
console.log(data.replace("$",""));
})
Here is the example of my CSV data:
Date, Close/Last, Volume, Open, High, Low
09/11/2020, $3116.22, 5093982, $3208.69, $3217.3409, $3083.98
09/10/2020, $3175.11, 5330741, $3307.219, $3349.89, $3170.55
09/09/2020, $3268.61, 5188675, $3202.99, $3303.175, $3185
And here is the error that I get:
SyntaxError: Unexpected number
at wrapSafe (internal/modules/cjs/loader.js:1072:16)
at Module._compile (internal/modules/cjs/loader.js:1122:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\jackl\Desktop\Projects\scraper_cheerio\scraper.js:5:14)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
I've figured out that Javascript perceives $3116 as a string and the following .22 as a property of that string object, which can not be a number and therefore throws "Unexpected number".
But how do I fix it? I've been trying to deal with it for 2 days, but no luck so far.
The problem was in this line:
const AMZN = require("./data/AMZN.csv");
The code in the question works fine.
i am trying execute test with jest in CRA and i keep getting this weird error.
C:\Users\mfonpah\Documents\projects\nexus-web-client\web-
client\node_modules\auth0-js\src\index.js:1
(function (exports, require, module, __filename, __dirname) { import
Authentication from './authentication';
^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Object.require.extensions.(anonymous function) [as .js]
(C:\Users\mfonpah\Documents\projects\nexus-web-client\web-
client\node_modules\babel-register\lib\node.js:152:7)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
any ideas on how to fix this?
thanks
If you see this error, your EF6 import statements are not being transpiled. Make sure your babel presets contain "env"
.babelrc and "babel" property in package.json
{
"presets":["env", "react"]
}
Depending on your use case you might also need to add the transform-es2015-modules-amd plugin, more here.
Why can't I print this simple json? jsonlint.com says this is valid
json:
[
{
"token_start_offset": "0.00",
"token_duration": "4.00",
"token_base_start_offset": "0.00",
"token_base_duration": "4.00",
"token_type": "background_noise",
"token_background_noise_type": "other",
"session_id": "1459194633575",
"token_base_form": "…",
"token_print_form": "…",
"session_boundary": "begin",
"nonspeech_boundary": "begin",
"token_id": "0"
}
]
app.js:
var testJson = require('./json');
console.log(testJson);
But when I run this, I get the below error:
Error:
module.js:428
throw err;
^
SyntaxError: C:\Users\Owner\Desktop\format test\json.json: Unexpected token
at Object.parse (native)
at Object.Module._extensions..json (module.js:425:27)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\Owner\Desktop\format test\app.js:1:78)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
Windows 10
node -v 4.2.6
Because the JSON parser in Node's require() assumes ASCII characters and your example contains a Unicode character: …. If you replace all instances of … with \u2026, your JSON should parse.
I'm sure I'm doing something stupid, but I can't get node-fnoc to work. I have a directory with a simple package.json in it, I have a /config/ directory in that directory with a simple JSON file in it. And, I'm basically just trying to do what they have in their example.
var fnoc = require('fnoc');
fnoc(function(err, configs){
console.log(configs);
});
and this is what I'm getting:
fnoc(function(err, configs){
^
TypeError: object is not a function
at Object.<anonymous> (/opt/backups/readconfig.js:3: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:901:3
Any ideas?
The document on github is for version 0.2.2
You need to install fnoc#0.2.2
npm install fnoc#0.2.2
Run your js
node your.js
Output:
{ package:
{ name: 'asdf',
dependencies:
{ express: '*',
jade: '*',
Version is in the changelog file:
https://github.com/jprichardson/node-fnoc/blob/master/CHANGELOG.md
I have a string which I would like to turn into an object so I can pass it to mongoose
string = "{setting: {foo: false}}"
options = JSON.parse(string)
but this is giving me this error
SyntaxError: Unexpected token s
at Object.parse (native)
at Object.<anonymous> (/Users/home/blah/blah/blah.js:48:20)
at Object.<anonymous> (/Users/home/blah/blah/blah.js:54:4)
at Module._compile (module.js:449:26)
at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:83:25)
at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:177:29)
at fs.stat.notSources.(anonymous function) (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:152:18)
at fs.readFile (fs.js:176:14)
at Object.oncomplete (fs.js:297:15)
any idea what I'm doing wrong?
The JSON isnt right.
strthing ='{"setting": {"foo": false}}';
options = JSON.parse(strthing);
alert(options.setting.foo); ----> False.
http://jsfiddle.net/eaXjk/