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

Middleware doesn't load relative to where you run the CLI from

Open 1j01 opened this issue 6 years ago • 4 comments

Issue description

If I try to use the --middleware option with a relative path, it fails, and it doesn't give much information. (It's hard to tell where it's searching from; it's buried in the middle of a stack trace.)

Error: Cannot find module 'extensionless-urls.js'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at /home/ubuntu/workspace/node_modules/live-server/index.js:174:10
    at Array.map (native)
    at Object.LiveServer.start (/home/ubuntu/workspace/node_modules/live-server/index.js:168:13)
    at Object.<anonymous> (/home/ubuntu/workspace/node_modules/live-server/live-server.js:166:12)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

I had to add ../../ to the --middleware option to get it to work, which is ugly. It seems like it should really look for files relative to where you launch the CLI from, i.e. the current working directory of the process.

Software details

  • Command line used for launching live-server: live-server --middleware=extensionless-urls.js
  • OS: Linux
  • Browser (if browser related):
  • Node.js version: v9.5.0
  • live-server version: 1.2.0

1j01 avatar Mar 20 '18 02:03 1j01

I've also been struggling with this issue.

The problem is especially prominent when running live-server through npx, in which case it is basically impossible to use non-builtin middleware without supplying an absolute path to the middleware file (which is undesirable as it is unlikely to work across different machines)

I'm setting up a small dev environment for our frontenders, and this is not possible without explicitly installing live-server in the project in order to supply the necessary middleware.

rasmniel avatar Oct 15 '19 13:10 rasmniel

@rasmniel If you're including middleware for live-server in the project, what's wrong with installing live-server in the project (as a dev dependency)?

1j01 avatar Oct 18 '19 16:10 1j01

Since my middleware is generic connect middleware, it could be used with any connect-compatible setup, e.g. express. This is why I don't want to explicitly add live-server.

Adding the dependency would be a workaround rather than a fix, which seems kinda half-hearted to me. There's so much "out-of-the-box" utility in this little package. Why not make it go a bit further with more reliable path resolution?

rasmniel avatar Oct 21 '19 07:10 rasmniel

I've made a pull request to remedy this problem #330

rasmniel avatar Oct 23 '19 13:10 rasmniel