No, you’re thinking clearly here. But we’d need to know how you’ve set up your project. Because:
require('lib')
is the function used by Node to find and evaluate a local JS file (a module).
But a browser knows absolutely nothing about Node or require or NPM. It doesn’t have access to the filesystem on some random computer. So you can write that code, but it won’t work in the browser.
For browsers, you want a very small amount of JS, maybe all shoved in a single file, and it gets to the browser via a <script>
tag in the HTML.
Actual native JS modules, they do work in the browser:
import * as $ from "./path/to/where/jquery/file/you/want.js"
Which, if, when you publish the website to a server, you also upload the dependencies, will work.
Installing things via npm then referencing them with direct paths to the exact files you want is not very useful though, which is why tools for bundling up JS you’ve installed/written in development into one file for you to use in a <script>
tag exist – WebPack, Vite, Parcel, ESBuild etc.
You write your code as modules, split across different files, and you can import
(or require
) stuff. Then you run the bundler and it complies all the stuff you’ve used/written down into one file (or files) you can reference in that script tag. Then when you put your stuff in a server, you can just upload the HTML, CSS and that file and off you go.
So, to reiterate, need to know how you’ve set up your project. You can do your “install jQuery with NPM, then require it in files that use it” (though note you proba ly want to be using the native import
/export
syntax, everything is moving to that). But to do that you need a tool set up to turn that into something more useful for the browser.