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

TypeError when launching JSON server

Open Ezcoo opened this issue 3 years ago • 6 comments

I get the following error when I try to launch JSON server (global install) with the following parameters: json-server -port=3002 --watch db.json

db.json is at the root of the folder the Windows Command Prompt is in.

  \{^_^}/ hi!

  Loading db.json
  Loading true
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received type
boolean (true)

    at Object.openSync (node:fs:577:10)
    at Object.readFileSync (node:fs:453:35)
    at C:\Users\Net_2\AppData\Roaming\npm\node_modules\json-server\lib\cli\run.js:118:32 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Ezcoo avatar Dec 09 '21 19:12 Ezcoo

It sounds like one of the top-level properties' value in your JSON file is a string, rather than an object or array. You can try wrapping your existing JSON in a top-level data property.

Before:

{
  "path": "/path/to/page",
  "id": 123
}

After:

{
  "data": {
    "path": "/path/to/page",
    "id": 123
  }
}

If you can start the server without error, you should be able to access http://localhost:3002/data to get your original data. If not, post your db.json here to help see what's going on.

saltymouse avatar Dec 12 '21 01:12 saltymouse

Hi,

this is my db.json. It's an example JSON file on a popular university course so I assume that it's correct (?):

{
  "persons":[
    { 
      "name": "Arto Hellas", 
      "number": "040-123456",
      "id": 1
    },
    { 
      "name": "Ada Lovelace", 
      "number": "39-44-5323523",
      "id": 2
    },
    { 
      "name": "Dan Abramov", 
      "number": "12-43-234345",
      "id": 3
    },
    { 
      "name": "Mary Poppendieck", 
      "number": "39-23-6423122",
      "id": 4
    }
  ]
}

Ezcoo avatar Dec 12 '21 22:12 Ezcoo

Sorry, I misread the original message. I thought it was similar to my own issue, as I outlined above, but I think it's not related to the db.json structure as your db.json works without error on my end (macOS 12, json-server 0.17.0, node 16). It might be worth linking to the repo you're working with so others can get a better idea of what's going on.

saltymouse avatar Dec 13 '21 04:12 saltymouse

Hey Were you able to resolve this issue ? The same is happening with me and I can't figure it out.

TarnjotKaur avatar Mar 13 '22 02:03 TarnjotKaur

@TarnjotKaur I don't remember exactly how I got it solved but if I recall it correctly, it was related to OS permissions, i.e. running the commands from restricted/non-admin/non-superuser account or from terminal/command prompt without superuser priviledges.

Ezcoo avatar Mar 19 '22 14:03 Ezcoo

For me what worked was to put my db.json file inside a folder named server. Then just json-server --no-cors --port 5000 --watch server/db.json

Not sure why this worked

gguarise avatar Aug 10 '22 02:08 gguarise

You have made an error in your command json-server --watch db.json -port 5000 you use a simple dash, where a double dash is required, for the --port argument.

t-ubukata avatar Oct 22 '22 09:10 t-ubukata