lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.23k stars 87 forks source link

Rewrite rules stopped working from npx on Windows #158

Closed kenlyon closed 3 years ago

kenlyon commented 3 years ago

I'm running on windows 10. I used to be able to supply command-line parameters for rewrite rules but today I ran into problems.

Windows is interpreting the ">" character in the rewrite rule as being the option to redirect output to a separate file, like you would here:

dir > files.txt

The most basic case that shows this is:

npx ws -r "/a -> /b"

The response I get is "Access is denied." which is Windows complaining about trying to write to /b.

There doesn't seem to be a way to escape the > character, so I've opted to use a lws.config.js config file instead, which solves the problem.

I'd suggest adding something about this to your wiki, as the Rewrite tutorial only mentions Windows needing double quotes instead of single quotes while in my case none of the inline examples work. It'd help others avoid the troubleshooting I had to go through.

Otherwise, I think your module is really useful. Thanks for making it!

75lb commented 3 years ago

Hi, can you confirm which shell you are using? I just ran the following command in both Git Bash and cmd on Windows 10 - everything worked correctly, as expected..

$ ws -r "/here -> /there" --verbose
{ 'middleware.cors.config': {} }
{
  'middleware.rewrite.config': { rewrite: [ { from: '/here', to: '/there' } ] }
}
{ 'middleware.conditional-get.config': {} }
{
  'middleware.static.config': { hidden: true, root: 'z:\\' }
}
{ 'middleware.index.config': { icons: true } }
kenlyon commented 3 years ago

I think I was using crd.exe, although I likely used the default shell in vs code as well.

I noticed you’re not running it via npx. Could that be why it works for you?

75lb commented 3 years ago

It could be an issue with npx or other Windows shells, then..

If you're having npx issues, this should work as an alternative:

./node_modules/.bin/ws -r "/here -> /there" --verbose

Closing, as the issue is not with local-web-server itself..

Venryx commented 3 years ago

I had the same issue on Windows, but fixed it with the workaround @75lb suggested. (although I needed to use backslashes rather than forward slashes)

Example:

.\node_modules\.bin\ws -r "/here -> /there" --verbose

So it appears the npx utility is not correctly handling either the quotes or the arrow. May be worth linking to this thread in the readme, for Windows users.

75lb commented 3 years ago

Could you confirm which version of npm you are using? I think this npx issue may have been fixed by this npm v7.4.0 PR..

Venryx commented 3 years ago

Ah, I am using NPM v6.14.5; the PR does look like it might fix it.

Unfortunately I don't have the motivation to confirm that at this point. (I don't want to risk ruining something in my npm dependency trees)

Because I can't confirm the issue for the latest versions (and because the linked PR probably fixes it), I'm fine with this issue being reclosed unless/until someone else can confirm it's still present in NPM v7.4.0+. (since there's a workaround for now anyway)

kenlyon commented 3 years ago

Despite the fault apparently being with npx, I still think it's worth considering updating the documentation to either mention the workaround or a minimum supported version in Windows. For me, I'm happy enough to use the separate config file. I think it's more maintainable in my case anyway.

Thanks for your help.

75lb commented 3 years ago

@kenlyon can you confirm this is still an issue with the lastest npm? As i posted above, this issue was apparently fixed in npm v7.4.0..

kenlyon commented 3 years ago

@75lb I can confirm that it does indeed work. Based on the Previous Releases page for node/npm, I confirmed it was still an issue with node 15.51.1 / npm 7.3.0. However, the next release (node 15.6.0 / npm 7.4.0) worked fine.

It's not really a viable solution for my use case, though, because we're using the LTS version and it wouldn't be trivial to switch to the "Current" track at this time. As I mentioned above, I'm fine with using the separate config file.

In addition, I confirmed it's still a problem in the latest LTS version. (node 14.17.0 / npm 6.14.13)

75lb commented 3 years ago

@kenlyon ok, thanks for confirming.. I have added a note to the top of the rewrite tutorial as suggested and will leave this issue open for a while. 👍