tapio / live-server

A simple development http server with live reload capability.
http://tapiov.net/live-server/
4.43k stars 484 forks source link

Live server fails to pull in .live-server.json on WSL #270

Open dvsoukup opened 6 years ago

dvsoukup commented 6 years ago
Before submitting an issue, please, see https://github.com/tapio/live-server#troubleshooting

Issue description

Currently I operate on WSL (Windows Subsystem for Linux), with ubuntu. During some project setup stuff, I opted to use live-server to serve out some html pages for a SPA I'm building.

Anyhow, per the documentation, it states that having a .live-server.json in the root of your project will utilize those values as a part of launching live-server. This failed time and time again though, and it did NOT consume the values in that JSON.

Software details

Other notes: In an attempt to just get this working locally, I wound up figuring out the issue here. So I have my VSCODE source files on the "windows" side, under the C drive. When using WSL, that C drive is technically mounted to /mnt/c/Users/me/path/to/project. However, the logic within live server checks which OS you're operating on, per line 17 of live-server.js:

var homeDir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];

For me it says I'm using a linux based system, thus it pulls in /home/me as the base directory to join to the .live-server.json file. Which is 100% wrong. I temporarily modified my local live-server.js to change it from:

var homeDir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
var configPath = path.join(homeDir , '.live-server.json');

to:

var configPath = path.join(process.cwd(), '.live-server.json');

This made it correctly consume my .json settings! Obviously this is a simple hack. I think there instead ought to be some sort of command line parameter to specify the path to where the .live-server.json file resides. If that command is ommitted, then just default to this current implementation of determining a users home directory. If the command is there, then traverse that path to read the .json file.

Would be useful to have a command such as: live-server --config=PATH, where PATH is the absolute path to where the .live-server.json file resides.

There might be better options but this is just a quick 5 min assessment of a way to solve this problem, so don't take this as "this is the way it must be done".

nkl3in commented 5 years ago

Assuming you are using the command to start live-server from the root directory of your project, you can prepend the HOME environment variable that will then be picked up by live-server. This will allow the .live-server.json file is to be correctly located and processed. I use a script in my package.json for this purpose:

{
  "scripts": {
    "dev": "HOME=./ node_modules/.bin/live-server"
}