Did you know that you can use any npm package in your project without having to install it or add it to the package.json. It’s true, sometimes we only want to test the package or perhaps we only need to execute the package once in a while. In both cases we may not want to pollute the dependencies under package.json.
There is a way where you can play with the package on the fly using the tool called npx, the Node Package Runner. Another cool thing is that this tool comes built in with the default npm installation so you don’t have to install anything extra.
Example: Run web-app on the fly
Lets say you are in root folder of the project. And you have already built the project and the compiled production files are residing in the dist folder. If you want to serve those files in a web server, typically you would need to install a web server locally and then host the files onto it before you can actually load the app in the browser.
Thanks to npx you don’t have to do all of that. Lets see how you can fetch the server on the fly from git, host the files and open the web app automatically, all using one simple command.
The server that we will be using is called lite-server. Simply using the terminal execute this command:
npx lite-server --baseDir="dist"
Give it a few seconds, and it will open the web-app automatically in the new tab of your browser. Pretty-cool!
[amazon_auto_links id=”529″]Wait, how did it work ..
npx lite-server: this will instruct npx to fetch the package lite-server from git and run it. Now this command can take extra arguments as one below.
–baseDir=”dist”: instructs the lite-server command to use the dist folder under the current project folder to serve the files for the web-app.
You can play with other config options available from lite-server. Happy coding!