36

I am setting up a json-server and I install it and assign it to be my npm start command like so

"scripts": {
    "start": "json-server -p 3001 -w db.json"

but every time I type npm start on the terminal I got this error

> api-server@1.0.0 start C:\Work\React-projects\streams\api-server
> json-server -p 3001 -w db.json

'json-server' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api-server@1.0.0 start: `json-server -p 3001 -w db.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api-server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Abdelaziz\AppData\Roaming\npm-cache\_logs\2019-04-06T09_07_30_796Z-debug.log

Abdelaziz Mohamed
  • 363
  • 1
  • 3
  • 8

7 Answers7

131

First, you need to check json-server installed globally or not. or you can install it globally by

npm install -g json-server

If you install it locally in your project, use npx to run it

npx json-server --watch db.json

Checkout difference between npx and npm here: https://stackoverflow.com/a/52018825/11285186

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
namth
  • 2,747
  • 1
  • 9
  • 17
13

First step you install the json server using the following command:
npm install -g json-server
Then, you can run the command and select the port using the following command:
npx json-server --watch -p 3333 server.json
Change 3333 to the port number you want and replace server.json with the name of your fake api.

In my case, I'm using React js and my server.json is at the root of my application. enter image description here

enter image description here

Everton Sales
  • 311
  • 3
  • 3
7
npm i -g json-server 

json-server --watch db.json

if this don't work

npm i -g json-server 

npx json-server --watch db.json

and this way works for mac

Nafiz Bayrak
  • 200
  • 2
  • 3
3

Update the node.js version, Works on versions above node.js 10 versions.

Salih ŞEKER
  • 199
  • 1
  • 3
3

I got into the Same Problem When I tried to add the json-server as a script in the package.json file so as to run it using

npm run start

got the error "json-server is not recognized as an internal or external command"

a quick solution is to Modify the scripts as

"scripts": { "start": "npx json-server -p 3001 -w db.json" }

another way is

"scripts": { "server" : "npx json-server --watch db.json --port 3001" }

1

You can try :

  1. npm i -g json-server
  2. go to your application path example:\angular\TestApps\src\assets\mockdata>
  3. run npx json-server --watch .\db.json Ex: here db.josn is my fake web api
  4. this will run json server on http://localhost:3000/ without any issue
Gaurav Joshi
  • 861
  • 1
  • 11
  • 17
0

The common mistake is not installing the json-sever package Globally !

What worked for me : I used the command :

npm install -g json-server

(-g is for globally installing a package)

For testing :

json-server --watch data/db.json --port 8000 (--watch is use to check for database file [here path - data data/db.json] and --port is used to set port manually [here 8000])

You can check data on port 8000 now,

http://localhost:8000/anything

(You can use npx also)

confused_
  • 1,133
  • 8
  • 10