What is the best way to make `npm prune --production` work in a recursive manner?

It seems like a simple task, yet I am unable to find any information online.

When running npm prune --production, it only removes the direct devDependencies from the current package's node_modules folder. Unfortunately, it does not recursively remove devDependencies down the entire node_modules tree. This results in my project being smaller due to the removal of direct dependencies, but it still contains unnecessary transitive devDependencies.

This behavior has been consistent across npm versions ranging from 6.4.1 to 6.14.4.

Answer №1

After some trial and error, I finally managed to figure it out without much assistance from the npm documentation.

If you want to install only dependencies (meaning only production dependencies) recursively, here are the two necessary steps:

  1. Firstly, run npm ci. This command installs the project's transitive dependencies' dependencies, including the direct devDependencies.
  2. Secondly, execute npm prune --production. This step removes the top-level project's devDependencies.

By following these instructions, you can significantly reduce your project's size, particularly useful for deployable projects like Docker images. Make sure to add

RUN npm ci && npm prune --production
in your Dockerfiles to ensure a smaller final image due to a minimized node_modules directory.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting Plain JSON Objects into a Hierarchical Folder Structure using Logic

Looking at the data provided below: [ {name: 'SubFolder1', parent: 'Folder1'}, {name: 'SubFolder2', parent: 'SubFolder1'}, {name: 'SubFolder3', parent: 'SubFolder2'}, {name: 'Document ...

Adding items to the array is only effective when done within the loop

My approach involves retrieving data from an API using axios, organizing it within a function named "RefractorData()," and then pushing it onto an existing array. However, I have encountered a problem where the array gets populated within a forEach loop, a ...

Leveraging the SNAPSHOT feature in a personal NPM repository, resembling the functionality

I currently have a basic setup that is working smoothly: Library A Library B publishing 1.0.0-SNAPSHOT -> Private Repository -> npm install Note that A and B are located on separate machines (imagine ...

Has anyone been able to establish a successful connection between a NodeJS project and SQL Server using AAD-Managed identity?

I came across some code from the Microsoft docs, but unfortunately, it doesn't seem to be functioning correctly. If anyone has any insights on this issue, I would greatly appreciate it. Furthermore, I am curious if it is even possible to achieve what ...

New Exploit Allows for Arbitrary Code Execution by Bypassing Sandboxes - The popular web template engine, Jade, has

I'm looking to create a todo app using node.js, express.js, and mongoDb. I've installed express and followed these steps: npm install -g express-generator cd todo_api npm install But encountered an error while running npm install. After runni ...

Exploring the power of promises in the JavaScript event loop

Just when I thought I had a solid understanding of how the event loop operates in JavaScript, I encountered a perplexing issue. If this is not new to you, I would greatly appreciate an explanation. Here's an example of the code that has left me scratc ...

Having trouble with the installation of Parcel bundler via npm

Issue encountered while trying to install Parcel bundler for my React project using npm package manager. The terminal displayed a warning/error message during the command npm i parcel-bundler: npm WARN deprecated [email protected]: core-js@<3 is ...

Trouble arises when attempting to Browserify build with a local version of an npm module listed in my package.json dependencies

After successfully setting up a package.json and bundling my app's dependencies with browserify, I encountered an issue when trying to replace one of the dependencies with a local forked copy, resulting in build failures. The following configuration ...

Masked input fails to accurately capture data

When utilizing VueJS together with the Quasar framework. An issue arises when using a masked q-input, where the value becomes incorrect after a toggle. For instance, if we start with a default value of: officeNum: 654321, mobileNum: 12345678, Then our ...

What sets apart a node.js express route from a controller in terms of functionality and purpose?

Are there any advantages or added power to using a traditional controller instead of an express route? If you have an express app and define models, does it automatically become an MVC application, or are there further steps needed? I can't help but ...

Error message in Linux for NodeJS global NPM package: "File or directory does not exist"

After setting up my Ubuntu 14.04 system, I installed nodejs and npm using the command: sudo apt-get install nodejs npm To ensure packages utilize the node interpreter instead of nodejs, I created a symlink with: sudo ln -s /usr/bin/nodejs /usr/local/bin ...

Performing a mass insertion of records in MySQL using Node.js with the added functionality of updating existing rows if there is a duplicate

I am currently facing a challenge while attempting to perform a bulk row insert using node-mysql. Typically, this process is straightforward; however, I need to apply a MySQL function to one of the columns. Usually, I would execute a query like the follow ...

Utilizing API routes within your React application

After creating my own API using nodejs, express and mongoDB, I successfully tested the routes with postman. However, I am struggling to integrate these routes into my react app for CRUD operations on the data. Despite having previous experience working wit ...

Setting up Visual Studio Code is a straightforward process that involves downloading the

I have been experimenting with visual studio code and attempting to utilize the built-in debugger. After finding some examples, I tried setting it up myself. Successfully, I managed to configure the gulp serve task which runs correctly using the command ...

Guide to hosting an expressjs application on shared cpanel hosting platform

I'm encountering an issue while attempting to deploy my basic express application on shared hosting cpanel. Whenever I try to access my app, it displays Cannot GET /apps/api/. Check out the screenshot of my setup below:- https://i.stack.imgur.com/cWYW ...

Is it feasible to utilize cloud functions for the purpose of transferring videos to the Firebase server?

I am exploring the option of writing a cloud function to download a video file. Below is an example of how I plan to save the file in mp4 format: const http = require('http'); const fs = require('fs'); const file = fs.createWriteStream ...

Can you provide some guidance on utilizing nested schemas in mongoose by incorporating 'type' to generate arrays?

I am in the process of developing a nested mongoose schema that utilizes the 'type' property to create a nested array structure. The specific schema I am encountering challenges with is "chorePerson". Below is the dataset that I am attempting t ...

When using Multer for file upload, the req.body returns as an empty object while req.file is undefined

I previously shared a problem I encountered while using multer for file upload in the MERN stack. Despite my attempts, I have not yet been able to resolve it. In my app, I am using both body-parser and multer. Here is the order of code in my index.js file: ...

Is there a way to specifically clear the cached view for a single file in express.js?

When I render my homepage using the following code: router.get('/', function(req, res, next) { res.render('../_cache/homepage-1.hbs', { title: 'Home', style: 'home-new', projectSlug: &apo ...

Integrating JavaScript into HTML pages with corresponding file names

I am looking for a way to dynamically insert a .js file into an HTML page with the same filename using gulp-inject (for example, injecting index.min.js into index.html and data.min.js into data.html). The minified files are located in build/js and the HTML ...