Setting baseDir in lite-server bs-config and still access node_modules
When using the popular lite-server NPM module (built on top of browsersync), let’s say that instead of the root folder of your project (the NPM project folder where package.json and node_modules resides), you want to put your code (index.html, index.js, etc) inside another folder. Let’s say you want to name this folder src.
According to the lite-server documentation, you can do this by creating a bs-config.json or bs-config.js file and setting a baseDir configuration object to ./src. Sounds simple, so you do that then you change all your links to node_modules to ../node_modules.
But what the docs don’t tell you is that lite-server/browsersync won’t let you use paths that go up the directory tree from baseDir, so you can’t access anything in the root folder of your project unless they’re in the new baseDir (src) folder.
Except by adding a route configuration object, like this:
You can create a bs-config.json file with this content (in the project root folder where you have package.json/node_modules):
{
"server": {
"baseDir": "./src",
"routes": { "/node_modules": "node_modules" }
}
}
Or you can create a bs-config.js file with this content:
module.exports = {
server: {
baseDir: './src',
routes: {
'/node_modules': 'node_modules'
}
}
}
Either will work. You can add routes for whatever folders you want to access that are outside baseDir. Then wherever you call lite-server, call it with lite-server -c bs-config.json or lite-server -c bs-config.js and you should be good to go.
The available documentation on how to do this is sparse, so hopefully this will help someone out.