question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"[ERROR] SyntaxError: Unexpected token : " when using non .json file ending for config files

See original GitHub issue
./node_modules/db-migrate/bin/db-migrate -e circleci --config database.json.sample  up
[ERROR] SyntaxError: Unexpected token :
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.exports.load (/..../node_modules/db-migrate/lib/config.js:22:14)
    at loadConfig (/..../node_modules/db-migrate/bin/db-migrate:117:12)
    at run (/..../node_modules/db-migrate/bin/db-migrate:319:7)

Using “version”: “0.9.23” according to package.json with node 4.4.

It works again when I do cp database.json.sample database.sample.json and re-run the command with the new config.

I double checked the json file it is not malformed.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wzrdtalescommented, Mar 18, 2016

@bf As the whole thing fails at require this does not belong to db-migrate, but to node js itself.

You would be able to reproduce this just by writing a single line of code yourself:

require('./data.json.sample');

And you would get your failure, if you change this and remove .sample everything works again.

So you may ask: What the hell is wrong with this? Well you just don’t know yet how require works.

Require loads a javascript module and has also another mode. Loading json files, but json files are only, and really only parsed if the file is named with the .json suffix. In all other cases require asssumes your file is a file containing javascript.

0reactions
wzrdtalescommented, Mar 19, 2016

Also to add, adding a custom error message to this would need to put try catch blocks around that require.

And also there is one thing that is against a fs.readFile + JSON.parse. No user would be able anymore to load a javascript file as configuration anymore. Because you could also create highly dynamic configs by requirering js configs which export the final configuration object.

Therefore, adding this feature would result in an unwanted behavior and a breaking change. There might be users that know about require and use filenames not ending with .js. So even if we consider to add to require non .js and non .json files with fs.readFile instead, we have to think about if it is worth to break the configs of many users.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Unexpected token < in JSON at position 0
Since it looks like the error seems to have something to do with pulling JSON data from the server, I tried starting from...
Read more >
Troubleshoot 'Uncaught SyntaxError: Unexpected token u in ...
An unexpected token simply means that it was expecting data, but instead got a command keyword (but this error can be triggered by...
Read more >
What Is JSON and How to Handle an “Unexpected Token” Error
In this article you will learn what JSON is and how you can deal with errors occurring when parsing JSON data, such as...
Read more >
How to fix Unexpected Token in JSON error (for ... - YouTube
Not a programmer? Read this! ** - If you get this error in an app you didn't make, and/or you're not a web...
Read more >
<!doctype is not valid json | The AI Search Engine You Control
I have already tried with the browser settings but the problem persists. ... But I'm getting this SyntaxError: Unexpected token '<', "<!DOCTYPE "......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found