6

I set a JSON file to use as a little practice database, but I can't run the server.

I've already tried to install (and reinstall) json-server global and locally npm install -g json-server and npm install json-server and then run json-server --watch db.json, but it doesn't work.

I've also tried to set a script in the package.json file "load": "json-server --watch db.json" and run the script node load.

But nothing seems to work and I keep getting in return the message:

"'json-server' command not found"

or, in Portuguese:

"'json-server' não é reconhecido como um comando interno ou externo, um programa operável ou um arquivo em lotes".

How can I fix this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Angelo Fonseca
  • 61
  • 1
  • 1
  • 3
  • if the `json-server` command isnt available in your terminal than the global npm packages are likely not being loaded correctly – Flame Mar 14 '19 at 18:45
  • Welcome to StackOverflow. Please attach the exact code you are trying to run to build a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Marcell Toth Mar 14 '19 at 18:45
  • 1
    Please edit your question rather than clarifying in the comment section:) – Marcell Toth Mar 14 '19 at 19:22
  • @MarcellTóth In the VSCode integrated terminal, I install json-server globally: npm install -g json-server I navigate to the db.json folder through the terminal by changing directory. Then I try to run the server: json-server --watch db.json However, I keep getting those massages in return, but npm and Node are working fine, the json-server folder is installed inside the node modules and as one of the dependencies in the package.json. I also tried to add a script to the package.json file: "load": "json-server --watch db.json" – Angelo Fonseca Mar 14 '19 at 19:25
  • @Flame I've just tested here. By getting the prefix and navigating to the folder, I find a json-server.cmd file there and I can force running the server there by typing `json-server.cmd --watch db.json`, but then I face another problem, because that prefix doesn't lead me to my projects directory and a simple copy and paste doesn't solve the problem. – Angelo Fonseca Mar 14 '19 at 19:31
  • @Flame Do you know how can I solve the problem and load the global npm packages correctly? – Angelo Fonseca Mar 14 '19 at 19:49

4 Answers4

27

Try:

npx json-server --watch db.json
Cristian Mora
  • 1,813
  • 2
  • 19
  • 27
4

Solution: For Linux:

  1. Firstly, run this command to globally install json-server

    sudo npm install -g json-server

  2. Move to your local folder(like my-app) and run this command

    npm install json-server

  3. Open a new Terminal in the same folder

    json-server --watch db.json --port 3004

Avani Khabiya
  • 1,207
  • 2
  • 15
  • 34
adithan bs
  • 41
  • 3
1

Check if this might help you.

Open the git bash(assume you have on your local machine) and execute below command: alias json-server="<..../node_modules/.bin/json-server.cmd>" ---> IGNORE <, > symbols and .... is the location where your node_modules folder is present

Then run json-server --watch db.json

Akshay
  • 33
  • 1
  • 7
1

I added the script for running the json-server in my package.json file like this:

 "server": "json-server db.json --watch --port 5000"

And tried to run it like this:

npm run server

And it works except the server wasn't being watched unless I had to manually end and run the json server command every time a change was made.

What fixed it for me was running the command directly in a bash terminal (under the app root directory).

So run:

json-server db.json --watch --port 5000
Victor Eke
  • 109
  • 6