Opt for utilizing a global npm package over repeatedly installing it

Is there a way to utilize the globally installed package without needing to reinstall it every time we run npm i? Here's the scenario I have: I've set up a docker image with a specific package already installed (installation command in the Dockerfile). When I start the container with the volume containing the JavaScript project and perform npm i, it appears that the package is being installed again.

Is it feasible to use the global package instead of installing it repeatedly?

Answer №1

To avoid re-installing node_modules during the docker build process, consider creating a base image that already includes them.

FROM node:18-alpine3.16
WORKDIR /usr/src/app
COPY package*  ./
RUN npm install 
docker build -t myimage .

You can tag and push this image to a registry like Dockerhub, or use it as your new base image locally.

FROM myimage
WORKDIR /usr/src/app #node_modules are already included here
COPY . .  # copy your code inside the image, or map the folder if you are in development

RUN npm run build # perform a build if needed
CMD ["mycommand","myargument"]

Keep in mind that you will need to rebuild your base image whenever you add or update node modules.

Consider using 'npm ci' instead of 'npm install' to maintain consistent versions of your node modules with each installation.

Stay vigilant about security vulnerabilities in both your node modules and base images.

Periodically run docker scan myimage to check for updates required by your node_modules or base images.

During development, it's perfectly acceptable to map your code folders with the docker image. You don't necessarily have to copy your code into the image while developing. Just map it to your WORKDIR.

Answer №2

My investigation led me to discover that this particular code snippet provides the solution I need

WORKDIR /directory

RUN npm install module
RUN npm cache add module
RUN rm -rf *

WORKDIR /  

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

Creating a carousel with three thumbnails arranged in two rows

Currently, I am using a slider for my thumbnails. If you want to check out the code for this thumbnail slider, click on this link: thumbnails slider code My goal is to display 6 thumbnails in total, with 3 thumbnails shown in each row (2 rows total). Add ...

Incorporating promises with ajax to enhance functionality in change events

Consider the scenario where you trigger an ajax request upon a change event in the following manner: MyClass.prototype.bindChangeEvent = function(){ $(document).on('change', '#elementid', function(){ var $element = $(this); $ ...

The Ionic framework has a defined variable

In my code, I have initialized a variable inside the constructor like this: constructor(public http: HttpClient) { this.data = null; this.http.get(this.url).subscribe((datas: any) => { this.dbUrl = datas[0].db_url2; console.log(this ...

Picture not showing up when loading iPhone video

My website features a video that is displayed using the following code: <div class="gl-bot-left"> <video controls=""> <source src="https://www.sustainablewestonma.org/wp-content/uploads/2019/09/video.fixgasleaks.mp4" ...

How can I access specific values from a table generated with a PHP array using jQuery?

Currently, I am working on a page where users can upload documents. Once uploaded, these docs are stored in a folder and their location is updated in a table for reference. Now, when I navigate to an edit page, I use PHP to query the database and display t ...

Tips for maintaining state URL persistence after a page refresh in Next.js 13 when utilizing next-usequerystate

Currently, I am using the next-usequerystate library with Next Js 13. However, I have encountered an issue where the state on the URL is lost when I refresh the page. The initial URL looks like this: localhost:3000/?page=1&genres=tree But upon refres ...

How can I connect the box using the Interactive Picture jQuery tool?

When using the Interactive picture jQuery, I have the following code snippet within my document: jQuery(document).ready(function(){ $( "#iPicture6" ).iPicture({ animation: true, animationBg: "bgblack", animationType: "ltr-slide ...

The OutlinedInput component from Material-UI seems to be struggling to display the startAdornment

Below is the code snippet. The start adornment is not displaying in the textfield, and there is no text appearing on the label. <InputLabel>Mobile Number</InputLabel> <OutlinedInput variant="outlined" ...

There was an error parsing the data from the specified URL (http://localhost:8000/src/client/assets/data.json

Hey there, I'm a newcomer to Angular and I'm having trouble reading a JSON array from a file. Every time I try, it gives me a "failed to parse" error. Can someone please provide some guidance? Here is my folder structure: src --assets ---a ...

I am looking to gather user input in JavaScript and then showcase that input on the webpage. What would be the best

I am currently learning Java and I decided to challenge myself by creating a hangman game. The issue I am facing is that when the user inputs a letter, nothing happens - there is no output indicating whether the guess was correct or incorrect. I suspect th ...

obtain the equivalent offsetX value for touch events as for mouse events

Greetings! I am currently attempting to retrieve the offsetX and Y values of a touch event, which ideally should match the offsetX value of a mouse event. In order to achieve this, I have implemented the following code: ev.offsetX = ev.targetTouches[0].p ...

Interacting with react-virtualized: accessing Grid's public functions

Trying to utilize the public method recomputeGridSize on a Grid component that I am currently rendering. I have set up the ref, but when attempting to call the method, it seems like it is not accessible. Outlined below is where the ref is established with ...

Tips for verifying the rendered view post data retrieval from an API in Vue JS

Having trouble retrieving data from the API using Vue JS and printing the page? After fetching the data, some elements may not render completely when trying to print, resulting in blank data being displayed. While using a setTimeout function may work for s ...

Utilizing Javascript / jQuery to eliminate specific CSS styles

I am facing an issue with the CSS code for a table positioned at the bottom of the screen. The current code includes a filter specifically for IE 8, but I need to make it compatible with IE 10 as well by removing the filter and adding a background color. ...

Retrieve a specific div element from the response data in AngularJS

Struggling to extract a specific div element from an AJAX response in Angular? Don't want the entire page content to be displayed? Tried various methods but nothing seems to work. Here's what I have attempted: $http({ method: 'GET&a ...

Troubleshooting issues with the delete functionality in a NodeJS CRUD API

I've been struggling to implement the Delete functionality in my nodejs CRUD API for the past couple of days, but I just can't seem to get it right. As I'm still learning, there might be a small detail that I'm overlooking causing this ...

What is the best way to send $(this) to a function?

Here is my code snippet: $(document).ready(function(){ var hCount = 0, eCount = 0, nCount = 0, mCount = 0; $("#head").click(function() { var pPos = calculatePosition(hCount); $(this).animate({left:pPos+"px"}, ...

Verify the Ajax submission of a post request prior to transmitting any data

Currently, I am utilizing Google Tag Manager to handle form submission inside a Bootstrap modal. Due to limited backend access, I have opted for GTB to target a specific button and utilize the onClick event. <script> $(document).on('submit&apos ...

Error in npm command: 404 Not Found - could not locate package "error-inject" version ^1.0.0

When attempting to run npm install, I encountered the following: $ node -v v11.7.0 $ npm -v 6.5.0 After running npm debug, an error occurs: npm verb stack Error: 404 Not Found: error-inject@^1.0.0 This error is related to a dependency of koa. Wha ...

Tips for avoiding passing an empty string when using local storage

I am currently using local storage to transfer two form inputs from a form on page A to a form on page B. The process is working smoothly, but I have encountered an issue. When I navigate directly to page B or visit it without inputting any data on page A, ...