how does one import nodejs modules in a vanilla Javascript file? In other words, how can I use my nodejs modules in a vanilla javascript file for frontend scripting?
how does one import nodejs modules in a vanilla Javascript file? In other words, how can I use my nodejs modules in a vanilla javascript file for frontend scripting?
You can include them via a CDN, like unpkg or cdnjs. It lets you easily load any file from any package using a URL like:
unpkg.com/:package@:version/:file
For example, to get d3 on your page, you could add a script tag like so:
<script src="https://unpkg.com/d3@5.5.0/dist/d3.min.js" />
These modules are packaged using a tool called npm. You can use module loaders or bundlers like Browserify or Webpack to use npm modules in the frontend.
This might help you
If you don't want to use the CDN, you can create a path with Node.js to get what you want from "node_modules"
in node.js
app.get('/jquery', (req, res) => {
res.sendFile(path.join(__dirname, 'node_modules/jquery/dist/jquery.min.js'))
})
in html
<script src="/jquery"></script>