typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.51k stars 6.99k forks source link

Again "json-server module not found" #1500

Open Drachenfels opened 7 months ago

Drachenfels commented 7 months ago

I am trying to follow one older tutorial: https://www.journeytoawebapp.com/posts/local-mock-server-with-node

Fresh repository I copy-pasted the code:

const jsonServer = require('json-server');
const server = jsonServer.create();
const middlewares = jsonServer.defaults();
const accounts = require('./responses/accounts/accounts');

const PORT = process.env.PORT || 3001;

server.use(middlewares);

server.get('/accounts', (req, res) => {
    res.jsonp(accounts);
})

server.listen(PORT, () => {
    console.log('Server running at http://127.0.0.1:' + PORT);
})

I did npm install json-server and I have this:

{
  "dependencies": {
    "json-server": "^1.0.0-alpha.22"
  }
}

But when I try to run the code with node server.js:

node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module 'json-server'
Require stack:
- <blip>/exp/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (<blip>/exp/server.js:1:20)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<blip>/exp/server.js' ]
}

My root:

$ ls -l
total 36
drwxrwxr-x 40 drachenfels drachenfels  4096 Jan 24 16:38 node_modules
-rw-rw-r--  1 drachenfels drachenfels    65 Jan 24 16:38 package.json
-rw-rw-r--  1 drachenfels drachenfels 22382 Jan 24 16:38 package-lock.json
-rw-rw-r--  1 drachenfels drachenfels   411 Jan 24 16:38 server.js

My node_modules:

$ ls -l node_modules/
total 148
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 anymatch
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 binary-extensions
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 braces
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 chalk
drwxrwxr-x  4 drachenfels drachenfels 4096 Jan 24 16:38 chokidar
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 dot-prop
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 es-escape-html
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 es-vary
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 eta
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 fill-range
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 glob-parent
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 header-range-parser
drwxrwxr-x  5 drachenfels drachenfels 4096 Jan 24 16:38 inflection
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 ipaddr.js
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 is-binary-path
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 is-extglob
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 is-glob
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 is-number
drwxrwxr-x  4 drachenfels drachenfels 4096 Jan 24 16:38 json5
drwxrwxr-x  5 drachenfels drachenfels 4096 Jan 24 16:38 json-server
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 lowdb
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 milliparsec
drwxrwxr-x  4 drachenfels drachenfels 4096 Jan 24 16:38 mime
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 mrmime
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 negotiator
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 normalize-path
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 picomatch
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 @polka
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 readdirp
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 regexparam
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 sirv
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 sort-on
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 steno
drwxrwxr-x 20 drachenfels drachenfels 4096 Jan 24 16:38 @tinyhttp
drwxrwxr-x  2 drachenfels drachenfels 4096 Jan 24 16:38 to-regex-range
drwxrwxr-x  4 drachenfels drachenfels 4096 Jan 24 16:38 totalist
drwxrwxr-x  3 drachenfels drachenfels 4096 Jan 24 16:38 type-fest

My NODE_PATH is empty (in case it's relevant):

echo $NODE_PATH
patrickpie83 commented 7 months ago

I have a similar error. Today I was trying to use json-server-auth, but end up with the error message.

Let me put the error first:

node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module 'json-server'
Require stack:
- /usr/local/lib/node_modules/json-server-auth/dist/guards.js
- /usr/local/lib/node_modules/json-server-auth/dist/bin.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/json-server-auth/dist/guards.js:6:20)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/lib/node_modules/json-server-auth/dist/guards.js',
    '/usr/local/lib/node_modules/json-server-auth/dist/bin.js'
  ]
}

Node.js v20.11.0

In my project, I use npm init to create package.json

then use npm install -D json-server json-server-auth to create node_modules, package-lock.json

and then create db.json file in my project

and use sudo npm install -g json-server-auth ,

then I finally use json-server-auth db.json, but it doesn't start the server, showed the Error: Cannot find module 'json-server'

package.json code

{
  "name": "vue-hangout-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "json-server": "^1.0.0-alpha.22"
  },
  "dependencies": {
    "express": "^4.18.2",
    "json-server-auth": "^2.1.0"
  }
}

I'm sure I install the latest version json-server and json-server-auth,

How can I make sure it finds module 'json-server'?

syukronarie commented 7 months ago

Looks like there is an issue in alpha version.

I can able to run normally using json-server@^0.17.3

filipopo commented 7 months ago

Can confirm, it works on 0.17.4 but not on the latest release

ritheesh4 commented 6 months ago

This issue exists for me also.

sjaelselement commented 6 months ago

Да, проблема в версии. npm i json-server@0.17.4 - решение

PaperPesto commented 5 months ago

Confirm, doesn't work with version 1.0.0-alpha.23

ritheesh4 commented 5 months ago

Correct. This issue is existing with the latest version. It does not there in "0.16.3".

lbombassei commented 5 months ago

Any ETA on fixing? It's great to have paging and sorting already done by json-server

Drachenfels commented 5 months ago

Any ETA on fixing? It's great to have paging and sorting already done by json-server

Tool feels to be if not abandoned then not under active development. So probably would need forking or something.

Denver-Ning commented 5 months ago

doesn't work with version "^1.0.0-alpha.23", I use "0.17.4" version is OK

MedvidekPu commented 4 months ago

Looks like there is an issue in alpha version.

I can able to run normally using json-server@^0.17.3

just spent 3 hours debugging this just to find out it was the version issue. Thank you so much for the answer

prasannabiswas commented 4 months ago

It happened with me also, it works properly with json-server@^0.17.4 and before versions but not on alpha versions. So i would recommend to use json-server@^0.17.4 versions

PouryaSoleimani commented 3 months ago

I'm also having exactly the same Issue , Should we just Downgrade our json-server-auth ? whould it be enough to solve the problem????

PouryaSoleimani commented 3 months ago

What about my json-server-auth ? which version of it should i install ??

prasannabiswas commented 3 months ago

Yes downgrade and use json-server@^0.17.4 versions. It will work absolutely without any problem with node

PouryaSoleimani commented 3 months ago

Thanks a lot @prasannabiswas , I did it and the problem is solved 😊

Yusei-0 commented 1 month ago

the solution is install the version "json-server": "^0.17.3", using pnpm i json-server@^0.17.3 or npm

miral-zymr commented 1 month ago

Confirming, it doesn't work with v^1.0.0-beta.1 as well.

TrungHieu0811 commented 6 days ago

Confirming, it doesn't work with v^1.0.0-beta.2 as well.