web development – correct way to serve the file that is in node_modules

Let's say I have this structure:

draft/
node nodes /
xxx /
asset.js
index.html
app.js

and in app.js I have:

const express = require (& # 39; express & # 39;);
const app = express ();

app.use (& # 39; / docs & # 39 ;, (req, res) => {
res.render (require.resolve (& # 39; ./ node_modules / xxx / index.html & # 39;));
});

app.listen (3000);

The problem is that the server is likely to run from the root directory of the project, the index.html file will not have the correct path to pick up the asset.js file.

For example, in index.html if the base is set like this:


That will not work. I could try using:


Another solution would be to group everything together so that the index.html file does not need to make any requests for static assets, but I am looking for a good solution to make sure that the resolution of the route works in a generic way. For example, if the Express server does not start in the root of the project, but in another subdirectory.