json-server icon indicating copy to clipboard operation
json-server copied to clipboard

why throw a error : The database is a JavaScript file but the export is not a function?

Open LuckyDing opened this issue 7 years ago • 5 comments

db.js module.exports = () => { const data = { users: [] } // Create 1000 users for (let i = 0; i < 1000; i++) { data.users.push({ id: i, name: user${i}}) } return data } index.js `const path = require('path') const jsonServer = require('json-server') const ip = require('ip').address() const db = require('./db')

const server = jsonServer.create() const router = jsonServer.router(db()) const middlewares = jsonServer.defaults()

server.use(middlewares) server.use(router) server.listen({ host: "localhost", port: 9090 }, function() { console.log(JSON Server is running in http://${ip}:9090) }) `

LuckyDing avatar Jan 26 '18 07:01 LuckyDing

I have the same error.

//server.js var jsonServer = require('json-server') var server = jsonServer.create() var router = jsonServer.router('db.json') var middlewares = jsonServer.defaults() server.use(middlewares) server.listen(3000, () => { console.log('JSON Server is running') })

//db.json { "user": [ { "id": 1, "nombre": "asdasd", "apellido": "Siasdasdlva", "mail": "asdads" } ] }

//routes.json { "/api/*": "/$1", "/:resource/:id/show": "/:resource/:id", "/api/user": "/usuario/1" }

when i execute #>json-server server.js --routes routes.json

{^_^}/ hi!

Loading server.js C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\utils\load.js:28 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 (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\utils\load.js:28:13) at start (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\run.js:122:5) at module.exports (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\run.js:159:3) at module.exports (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\index.js:81:3) at Object. (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\bin.js:3:14) at Module._compile (module.js:643:30) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3)

//================================================================//

if execute "node server.js --routes routes.json"

Basic server works but custom routes are ignored.

some idea?

lautaros86 avatar Feb 06 '18 12:02 lautaros86

facing the same issue. Any help?

harshal-wani avatar Mar 29 '19 03:03 harshal-wani

Same issue is facing on my end. How can resolve this issue?

pravin02pal avatar May 03 '19 06:05 pravin02pal

Same issue here.

Surdok avatar Aug 28 '19 10:08 Surdok

same issue

lbqfsj avatar Aug 11 '22 07:08 lbqfsj