json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Remove errorhandler dependency because it is causing issues when bundling

Open casertap opened this issue 6 years ago • 6 comments

I would like to bundle my json-server using webpack. I have a very simple webpack config, my express server is working well with the command:

node fake-backend/babel.server.js

As soon as I bundle and run the server again I get those errors:

node dist/server.js
fs.js:646
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open '/public/style.css'
    at Object.fs.openSync (fs.js:646:18)
    at Object.fs.readFileSync (fs.js:551:33)
    at Object.<anonymous> (/................/dist/server.js:40247:21

It is the package:

─┬ [email protected]
  └── [email protected]

that is loading some static files like that:

var DOUBLE_SPACE_REGEXP = /\x20{2}/g
var NEW_LINE_REGEXP = /\n/g
var STYLESHEET = fs.readFileSync(path.join(__dirname, '/public/style.css'), 'utf8')
var TEMPLATE = fs.readFileSync(path.join(__dirname, '/public/error.html'), 'utf8')
var inspect = util.inspect
var toString = Object.prototype.toString

Because it is not using require webpack is not able to find them.

casertap avatar Sep 17 '18 11:09 casertap

An easy fix for this one would be to push the const errorhandler = require('errorhandler') inside the conditional where is it used:

if (process.env.NODE_ENV === 'development') {
 // only use in development
 const errorhandler = require('errorhandler')
 arr.push(errorhandler())
 }

casertap avatar Sep 17 '18 11:09 casertap

here is a PR: https://github.com/typicode/json-server/pull/856

casertap avatar Sep 17 '18 11:09 casertap

I hitting this issue while bundling this with webpack. Any way this could be fixed on the client without making change to json-server?

For now I can avoid bundling the node modules.

vreddi avatar Apr 09 '19 01:04 vreddi

I've been able to work around this by declaring errorhandler as a string external in the webpack config:

const webpackConfig = {
  externals: [
    { 'errorhandler': '{}' },
    ...
  ],
  ...
}

From reading the docs, I think this is replacing refs to errorhandler with an empty object, and I assume the uses of errorhandler are simply failing silently.

In any case, I can now run the bundle without the errorhandler error, using node dist/index.js.

jpiersol avatar Jan 14 '21 21:01 jpiersol

If the above solution does not work, I solved this issue by declaring the entire json-server as an external to the webpack bundle, rather than solely the transitive dependency of errorhandler.

anmilleriii avatar Oct 14 '21 01:10 anmilleriii

If you happen to be using Angular to bundle this, you can get to webpack externals for build-angular:server builder using "externalDependencies": ["errorhandler"] to prevent the static files error from being thrown. Additionally, if you have the option to not bundle dependencies, you can set "bundleDependencies": false and the public directory will be found in node_modules.

It would be great to have an option to run this without the static files. It's nice to have an index page, but this package is geared towards making API calls, which don't need stylesheets or HTML to function.

robertabramski avatar Oct 26 '21 03:10 robertabramski