Search code examples
javascriptjson-server

How do I fix error message while trying to run json-server


After installing the json-server package, while trying to run it on my terminal, I got an error message. I installed the server using the command below:

npm install -g json-server

I then proceeded in running the server using the command below:

json-server -p 8080 -w ./data/db.json

I expected the terminal to return a link to API endpoint but I got the error message below:

/mnt/c/Users/InternetOG/AppData/Roaming/npm/node_modules/json-server/lib/bin.js:2
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
^

SyntaxError: Unexpected token {
at Module.\_compile (internal/modules/cjs/loader.js:723:23)
at Object.Module.\_extensions..js (internal/modules/cjs/loader.js:789:10)  
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module.\_load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)

Solution

  • I needed to prepend "npx" before running the json-server.

    npx json-server -p 8080 -w ./data/db.json
    

    This way I would get the expected result.