Json-server: Error: The database is a JavaScript file but the export is not a function.

Created on 25 Apr 2016  路  3Comments  路  Source: typicode/json-server

Using the simple example of db.json containing
{ "posts": [ { "id": 1, "title": "json-server", "author": "typicode" } ], "comments": [ { "id": 1, "body": "some comment", "postId": 1 } ], "profile": { "name": "typicode" } } and then making server.js containing as per the example in module

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)
server.listen(3000, function () {
  console.log('JSON Server is running')
})

and then running json-server server.js
the error is shown

  \{^_^}/ hi!

  Loading server.js
/usr/local/lib/node_modules/json-server/src/cli/utils/load.js:22
      throw new Error('The database is a JavaScript file but the export is not a function.')
      ^

Error: The database is a JavaScript file but the export is not a function.
    at module.exports (/usr/local/lib/node_modules/json-server/src/cli/utils/load.js:22:13)
    at start (/usr/local/lib/node_modules/json-server/src/cli/run.js:99:5)
    at module.exports (/usr/local/lib/node_modules/json-server/src/cli/run.js:126:3)
    at module.exports (/usr/local/lib/node_modules/json-server/src/cli/index.js:86:3)
    at Object.<anonymous> (/usr/local/lib/node_modules/json-server/bin/index.js:2:22)
    at Module._compile (module.js:413:34)
    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 Function.Module.runMain (module.js:447:10)

If I am doing something wrong, perhaps the documentation is not clear enough...

Most helpful comment

is there any way to start json-server with node server.js and also apply CLI options such as delay?

All 3 comments

Hi @Isanderthul,

Thank you for reporting this issue. Simply start your server using the following command-line:

node server.js

Or even better using nodemon to automatically restart server on change.
I'll update the README to make it clearer how to start the project.

Thank you!

is there any way to start json-server with node server.js and also apply CLI options such as delay?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fishenal picture fishenal  路  3Comments

dotmobo picture dotmobo  路  4Comments

strom picture strom  路  4Comments

boydenhartog picture boydenhartog  路  3Comments

jasonlimantoro picture jasonlimantoro  路  4Comments