Errors and warnings caught off guard while running json-server with the --watch flag

I'm having some trouble using json-server in the following way:

$ json-server --watch db.json

Every time I try to run that command, I encounter errors or warnings depending on the version of json-server that is installed:

  • 1.0.0-alpha.1-1.0.0-alpha.12:

    sh: json-server: command not found
    

    or (if on Windows):

    The term 'json-server' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + json-server --watch db.json
    

    or (when executed via npx):

    npm ERR! could not determine executable to run
    
  • 1.0.0-alpha.13:

    node:internal/errors:496
        ErrorCaptureStackTrace(err);
        ^
    
    TypeError [ERR_PARSE_ARGS_UNKNOWN_OPTION]: Unknown option '--watch'. To specify a positional argument starting with a '-', place it at the end of the command after '--', as in '-- "--watch"
    
  • 1.0.0-alpha.14+:

    --watch/-w can be omitted, JSON Server 1+ watches for file changes by default
    
  • 1.0.0-alpha.13+, for users of Node.js versions prior to v18.3.0 and v16.17.0:

    import { parseArgs } from 'node:util';
             ^^^^^^^^^
    SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
    

Here's a simple package file (remember to update the version of json-server accordingly):

{
  "name": "q77787616",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "json-server --watch db.json"
  },
  "keywords": [],
  "license": "ISC",
  "dependencies": {
    "json-server": "1.0.0-alpha.12"
  }
}

Answer №1

json-server is currently undergoing active development towards reaching version 1. However, these alpha versions are being released on npm with the latest tag, causing them to be installed instead of the stable version (currently 0.17.4) when simply running npm install json-server. This has led to several issues:

  • Prior to version alpha.13, the correct binary was not installed at all, resulting in the inability to find the json-server command (typicode/json-server#1472).

  • With version alpha.13, the binary was included but the CLI changed, making --watch an invalid argument (typicode/json-server#1474):

    $ npx [email protected] --help
    Usage: json-server [options] <file>
    Options:
      -p, --port <port>  Port (default: 3000)
      -h, --host <host>  Host (default: localhost)
      -s, --static <dir> Static files directory (multiple allowed)
      --help  Show this message
    
  • Starting from version alpha.14, --watch is supported but not necessary, so the message has been reduced to a warning.

To check the version of json-server you have installed, use:

npm ls json-server

Due to the ongoing development and alpha status, it is recommended to explicitly install the stable version (the documentation can still be found here):

$ npm install json-server@0

Alternatively, if you wish to use alpha version 1 and are using a compatible Node.js version (i.e. ^16.17 || >=18.3), you can run npm install json-server@latest to get the latest version (make sure you have at least version alpha.14) and use the command without the --watch flag:

$ json-server db.json

Answer №2

To begin, confirm the version of the installed package. If you have version 1.0.0-alpha.23, there is no necessity to include --watch. Additionally, ensure that you generate the db.json document in the principal folder of your project. Upon completing this step, execute the subsequent command:

json-server db.json

Answer №3

Start by launching the "CMD" tool.

Next, run the command

npm install -g <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="19736a7677346a7c6b6f7c6b592937282e372d">[email protected]</a>
.

After that, enter the command json-server --watch db.json.

Find more information at: https://github.com/typicode/json-server/tree/v0

Answer №4

If you encounter issues with updating [email protected] within your current node_modules directory, the recommended steps are to delete the existing node_modules folder and then proceed with installing a stable version of json-server.

Type this command in your terminal: npm i <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f9e0fcfdbee0f6e1e5f6e1d3a3bda2a4bda7">[email protected]</a>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent certain dates from being selected in a designated input field

I am facing an issue with disabling certain array dates for a specific input field in a datepicker calendar. Even though I have included the script to exclude those dates, they are not getting disabled for that particular input field. html <input cla ...

What is the best way to choose the current Div's ID, as well as its Width and Height properties?

Within this section, there are four div elements with varying widths, heights, and colors that appear and disappear when their respective buttons are clicked. I am adding an "activeDiv" class to the visible div in order to easily select it using that class ...

Find elements that are not contained within an element with a specific class

Imagine having this HTML snippet: <div class="test"> <div class="class1"> <input type="text" data-required="true"/> </div> <input type="text" data-required="true"/> </div> I'm looking to select ...

Maintaining Object Position in 2D Transforms

I am trying to create multiple perspective-transformed rectangles in the lower right corner of a canvas by using ctx.transform: ctx.transform(1, 0, -1, 1, 10, 10). My goal now is to adjust the size of the drawing based on a variable scale=n, while keeping ...

What is the best way to refine the results from an AJAX request in Datatables?

I have successfully configured a Datatables plugin, set up a new table, and populated it with content using an AJAX call: var table= $("#mytable").DataTable({ ajax: "list.json", columns: [ {"data": "name"}, {"data": "location"}, ...

Utilizing environmental variables in npm scripts across different operating systems

As I develop a package.json file and utilize "npm run" to execute various scripts, I refer to the documentation here: https://docs.npmjs.com/misc/scripts. One of my scripts requires the expansion of environment variables and I aim to ensure it is compatib ...

Why is it necessary to use 'then' on the response JSON when using the Fetch API? It's like trying to decipher the hidden meaning

While delving into the realm of promises, I decided to test it out with a basic GET request on Twitch. Yet, one aspect is still puzzling me - why does json() return a promise? The response already contains the data, so what's the deal with it being wr ...

Java servlet, Selenium, and JavaScript are a powerful combination of tools that can

I am facing a situation where I need Selenium webdriver to be executed on the client side. On a webpage, I have a form with a Submit button inside it. The action attribute of the form calls a servlet named "servletName". Inside the servlet, the followin ...

What method can I use in pure Javascript to identify which day the week begins on, either Monday or Sunday, based on the

Can someone help me determine the start day of the week in local time using only Javascript? The start day could be Monday, Sunday, Saturday, or Friday. I came across this helpful resource on Stack Overflow: <firstDay day="mon" territories="001 AD AI ...

Updates to class variable values in jQuery are failing to take effect

Attempting to create my first JavaScript class has presented some challenges, specifically when it comes to updating a class variable. Despite hours of testing different approaches, I still can't seem to get it right! function ClassName(productId) { ...

The npm local server is serving up the incorrect project

As someone who is new to JavaScript development, I am seeking guidance on running a next.js project on my local machine. Within my /projects folder, there are multiple projects... /projects/project1 (SvelteKit) /projects/project2 (Next.js) Upon navigat ...

Once the "Get Route" button is pressed, I want to save my JavaScript variable into a database

I am seeking to automatically extract data from the Google Maps API and store it in my MySQL database. Specifically, I want details such as source address, destination address, distance, and duration for all available routes to be inserted into my database ...

Retrieving component layout from file

Storing the component template within inline HTML doesn't seem very sustainable to me. I prefer to load it from an external file instead. After doing some research, it appears that utilizing DOMParser() to parse the HTML file and then incorporating th ...

Exploring the World of Next.js and Sequelize Model Relationships

Greetings to all the problem-solving enthusiasts out there! I'm currently in the process of revamping a previous project using Next.js, and I've hit a roadblock that has me stumped. My current challenge involves establishing an association betwe ...

Adjust background color on click using JavaScript

Could someone provide me with the JavaScript code to change the background color of a page from blue to green when a button is clicked? I have seen this feature on many websites but haven't been able to write the code myself. I am specifically lo ...

"Eliminate the headers of columns within the collapsible rows on the ui-grid interface

I am working with an expandable table and trying to figure out how to hide the column headers for only the expandable rows within the table. I experimented with including showHeader : false in the subGridOptions, but without success as the headers are stil ...

Once the div content is reloaded using AJAX, the refreshed HTML suddenly vanishes

My JS code reloads the div every 2 seconds: var auto_refresh = setInterval(function() { $('#indexRefresh').load('/includes/index_refresh_include.php?_=' + Math.random()); }, 2000); After that, I have an AJAX request that loads mor ...

Querying through a database containing 1 million <string Name, int score> pairs efficiently within sub-linear time

My JSON object holds 1 million pairs. var student = {[ { name: "govi", score: "65" }, { name: "dharti", score: "80" }, { name: "Akash", score: "75" },............. up to a million ...

Unable to fetch the identification number from the database

I'm encountering an issue with retrieving the ID from my database: Here is a snapshot of my database: Below is my event controller class: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Event ...

elimination of nonexistent object

How can I prevent releasing data if two attributes are empty? const fork = [ { from: 'client', msg: null, for: null }, { from: 'client', msg: '2222222222222', for: null }, { from: 'server', msg: 'wqqqqqqqq ...