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

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

Open dvsoukup opened this issue 5 years ago • 1 comments

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

  • Command line used for launching live-server: ./node_modules/.bin/live-server

  • OS: Windows 10, but using Ubuntu 16.04 on WSL

  • Browser (if browser related):

  • Node.js version: 10+

  • live-server version: live-server 1.2.0

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".

dvsoukup avatar Jul 24 '18 17:07 dvsoukup

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"
}

nkl3in avatar Sep 15 '19 00:09 nkl3in