Json-server: Cannot find module 'json-server'

Created on 18 Jan 2017  ·  10Comments  ·  Source: typicode/json-server

I'm following the guide in README.md and I encountered error
Error: Cannot find module 'json-server' at Function.Module._resolveFilename (module.js:440:15) at Function.Module._load (module.js:388:25) at Module.require (module.js:468:17) at require (internal/module.js:20:19) at Object.<anonymous> (/Users/amycheong/Desktop/remote-json-server/dev/server.js:1:80) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3)'

when I node server.js

I have installed json-server globally.

Most helpful comment

mmm .. ok, try to install json-server locally (not globally)

  1. go to your server.js directory and run npm init -y
  2. npm install json-server --save-dev

--

You also could try:

  1. go to your server.js directory and run npm link json-server
  2. run node server.js

--

aside from that, I highly recommend you to change your global node_modules directory by setting an Environment variable NODE_PATH to /usr/lib/node_modules

export NODE_PATH=/usr/lib/node_modules

and then try to reinstall json-server (npm uninstall -g json-server && npm install -g json-server)

All 10 comments

Same here.

Please post your implementation here (server.js content) if possible.

@Nilegfx , thanks for replying.
I'm using the same server.js shown on README.md:

// server.js
var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)
server.listen(3000, function () {
  console.log('JSON Server is running')
})
$ node server.js

which results in

module.js:472
    throw err;
    ^

Error: Cannot find module 'json-server'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/fellipe/fake-server/server.js:1:80)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)

I am assuming that you are missing a very very very simple and obvious step with I can't guess, so I'll write down the steps (sorry if it is super obvious for you) but just to make sure that we are in the same line.

  1. create a new directory (in your case I see you called it fake-server)
  2. copy the code snippet in server.js file
  3. run npm install -g json-server
  4. run node server.js

if you did the above and it is still not running, from the same directory that server.js file exist, run
which json-serverand share the result of this command with us here.

Ok, thats probably some detail on my side...

  1. create a new directory (done)
  2. copy the code snippet (done)
  3. run npm install json-server --g (seems ok, output below)
$ npm install json-server --g
/home/fellipe/.npm-global/bin/json-server -> /home/fellipe/.npm-global/lib/node_modules/json-server/bin/index.js
/home/fellipe/.npm-global/lib
└── [email protected] 
  1. run node server.js (bad, output below)
$ node server.js 
module.js:472
    throw err;
    ^

Error: Cannot find module 'json-server'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/fellipe/fake-server/server.js:2:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
  1. $ which json-server
$ which json-server
/home/fellipe/.npm-global/bin/json-server
  1. $ node --version = v7.5.0
  2. $ npm --version = 4.2.0

did you try sudo node server.js?

Yep :(

mmm .. ok, try to install json-server locally (not globally)

  1. go to your server.js directory and run npm init -y
  2. npm install json-server --save-dev

--

You also could try:

  1. go to your server.js directory and run npm link json-server
  2. run node server.js

--

aside from that, I highly recommend you to change your global node_modules directory by setting an Environment variable NODE_PATH to /usr/lib/node_modules

export NODE_PATH=/usr/lib/node_modules

and then try to reinstall json-server (npm uninstall -g json-server && npm install -g json-server)

Thanks a lot @Nilegfx, you solved it!

I've installed locally as you said (first 1.2.) and it worked. Then I've removed the local node_modules/and the package.json, created the NODE_PATH env var, installed globally as I was doing earlier and it worked too.

Glad to hear that, and BTW, the local installation didn't fix it, the NODE_PATH variable did.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

goldmont picture goldmont  ·  3Comments

zenith77 picture zenith77  ·  3Comments

casvil picture casvil  ·  4Comments

melnikovic picture melnikovic  ·  3Comments

boydenhartog picture boydenhartog  ·  3Comments