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.
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.
fake-server) server.js filenpm install -g json-servernode server.jsif 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...
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]
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)
$ which json-server$ which json-server
/home/fellipe/.npm-global/bin/json-server
$ node --version = v7.5.0$ npm --version = 4.2.0did you try sudo node server.js?
Yep :(
mmm .. ok, try to install json-server locally (not globally)
npm init -ynpm install json-server --save-dev--
You also could try:
npm link json-servernode 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.
Most helpful comment
mmm .. ok, try to install
json-serverlocally (not globally)npm init -ynpm install json-server --save-dev--
You also could try:
npm link json-servernode server.js--
aside from that, I highly recommend you to change your global node_modules directory by setting an Environment variable
NODE_PATHto/usr/lib/node_modulesexport NODE_PATH=/usr/lib/node_modulesand then try to reinstall json-server (
npm uninstall -g json-server && npm install -g json-server)