Node.js

How to include scripts located inside the nodemodules folder

25 September 2026 · 7 min read

How to include scripts located inside the nodemodules folder

Managing JavaScript dependencies efficiently is crucial for modern web development. When working with Node.js and npm (Node Package Manager), you’ll often find yourself needing to include scripts located within the node_modules folder. This folder houses all your project’s external JavaScript libraries, but directly referencing files from it can lead to issues with organization, maintainability, and deployment. Understanding how to correctly include these scripts is a fundamental skill for any web developer. This article explores various approaches and best practices for seamlessly integrating these scripts into your projects, ensuring a smooth development workflow and optimized website performance.

Using a Build Tool (Webpack/Parcel/Rollup)

Modern JavaScript development heavily relies on build tools like Webpack, Parcel, or Rollup. These tools bundle your JavaScript modules, including those from node_modules, into optimized files for your browser. This approach offers significant advantages in terms of performance, code organization, and maintainability. Build tools handle dependency resolution, code splitting, and minification, ultimately leading to faster loading times and a better user experience.

For example, with Webpack, you can import modules from node_modules directly into your JavaScript files using import statements. Webpack’s configuration handles the rest, bundling the necessary dependencies into your final output. This modular approach promotes code reusability and makes managing complex projects significantly easier. It’s the recommended approach for most modern web applications.

Furthermore, build tools enable the use of advanced features like tree shaking, which eliminates unused code from your bundles, further optimizing your application’s size and performance.

Using a CDN (Content Delivery Network)

For commonly used libraries like jQuery or React, using a Content Delivery Network (CDN) is a viable option. CDNs host these libraries on servers distributed globally, reducing latency and improving loading times for users worldwide. This is especially beneficial if the library is widely used, as the user’s browser might already have it cached from other websites.

Including a script from a CDN is simple. Just add a <script> tag to your HTML, pointing to the CDN URL of the library. However, relying solely on CDNs for all dependencies can be risky. You’re dependent on the CDN’s availability and performance, which can impact your website’s reliability. It’s generally best to use CDNs for widely used, stable libraries and consider other approaches for project-specific dependencies.

Example:

<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>

While technically possible, directly referencing files within node_modules using relative paths is generally discouraged. This approach can lead to brittle code that’s difficult to maintain and refactor. Furthermore, it can create issues when deploying your application, as the directory structure might change.

For example, a path like ../node_modules/library/dist/library.js can easily break if the project structure is modified. It also exposes your project’s internal structure, which might be a security concern in certain cases. While this method might be tempting for quick prototyping or very small projects, it’s best to avoid it for production applications.

Alternatives like using build tools or CDNs provide more robust and maintainable solutions for managing external JavaScript dependencies.

Best Practices for Managing Node Modules

Keeping your node_modules folder organized and efficient is essential for smooth development. Regularly updating your dependencies ensures you have the latest features and security patches. Tools like npm outdated can help identify outdated packages. Additionally, consider using npm prune to remove unused packages, keeping your node_modules directory lean and efficient.

  • Regularly update dependencies.
  • Use npm outdated and npm prune.

Following these best practices will contribute to a more streamlined and maintainable project structure. It also helps reduce potential conflicts and ensures optimal performance.

Choosing the right method depends on your project’s specific needs and scale. For smaller projects, a CDN might suffice. However, for larger, more complex applications, using a build tool offers the greatest flexibility, performance, and maintainability.

  1. Analyze your project’s requirements.
  2. Choose the most suitable method (build tool, CDN, or direct referencing – with caution).
  3. Implement and test your chosen approach.

An infographic would be placed here illustrating the different methods and their pros/cons.

  • Build tools offer optimal performance and maintainability.
  • CDNs are suitable for common libraries.

By understanding these different approaches, you can effectively manage your JavaScript dependencies and build high-performance web applications. Explore the documentation of popular build tools like Webpack, Parcel, and Rollup for more in-depth information and configuration options. Remember to prioritize build tools for larger projects to leverage their powerful features for optimization and code organization. Check out this helpful resource on module bundlers: Webpack Modules.

This blog post explores the intricacies of including scripts from your project’s node_modules directory. We’ve covered different methods, from utilizing powerful build tools like Webpack and Parcel to leveraging the speed of CDNs, and even touched upon the less recommended practice of direct referencing. Remember, choosing the right strategy depends on your project’s specific needs, with build tools generally preferred for larger applications and CDNs offering a convenient solution for commonly used libraries. Regularly updating dependencies and using tools like npm outdated and npm prune is crucial for maintaining a clean and efficient project. Learn more about optimizing your workflow here. By mastering these techniques, you’ll ensure smoother development, optimized performance, and a more maintainable codebase. Now that you’re equipped with this knowledge, put it into practice and elevate your web development process. Explore further resources like the official Node.js documentation and npm documentation to delve deeper into dependency management.

FAQ

Q: What is the node_modules folder?

A: The node_modules folder is where npm (Node Package Manager) stores all the external JavaScript libraries (dependencies) that your project requires. These dependencies are essential for your project to function correctly.

Question & Answer :
I have a question concerning best practice for including node_modules into a HTML website.

Imagine I have Bootstrap inside my node_modules folder. Now for the production version of the website, how would I include the Bootstrap script and CSS files located inside the node_modules folder? Does it make sense to leave Bootstrap inside that folder and do something like the following?

<script src="./node_modules/bootstrap/dist/bootstrap.min.js"></script> 

Or would I have to add rules to my gulp file which then copy those files into my dist folder? Or would it be best to let gulp somehow completely remove the local bootstrap from my HTML file and replace it with the CDN version?

Usually, you don’t want to expose any of your internal paths for how your server is structured to the outside world. What you can is make a /scripts static route in your server that fetches its files from whatever directory they happen to reside in. So, if your files are in "./node_modules/bootstrap/dist/". Then, the script tag in your pages just looks like this:

<script src="/scripts/bootstrap.min.js"></script> 

If you were using express with nodejs, a static route is as simple as this:

app.use('/scripts', express.static(__dirname + '/node_modules/bootstrap/dist/')); 

Then, any browser requests from /scripts/xxx.js will automatically be fetched from your dist directory at __dirname + /node_modules/bootstrap/dist/xxx.js.

Note: Newer versions of NPM put more things at the top level, not nested so deep so if you are using a newer version of NPM, then the path names will be different than indicated in the OP’s question and in the current answer. But, the concept is still the same. You find out where the files are physically located on your server drive and you make an app.use() with express.static() to make a pseudo-path to those files so you aren’t exposing the actual server file system organization to the client.


If you don’t want to make a static route like this, then you’re probably better off just copying the public scripts to a path that your web server does treat as /scripts or whatever top level designation you want to use. Usually, you can make this copying part of your build/deployment process.


If you want to make just one particular file public in a directory and not everything found in that directory with it, then you can manually create individual routes for each file rather than use express.static() such as:

<script src="/bootstrap.min.js"></script> 

And the code to create a route for that

app.get('/bootstrap.min.js', function(req, res) { res.sendFile(__dirname + '/node_modules/bootstrap/dist/bootstrap.min.js'); }); 

Or, if you want to still delineate routes for scripts with /scripts, you could do this:

<script src="/scripts/bootstrap.min.js"></script> 

And the code to create a route for that

app.get('/scripts/bootstrap.min.js', function(req, res) { res.sendFile(__dirname + '/node_modules/bootstrap/dist/bootstrap.min.js'); });