How can I optimize puppeteer for deployment in a production environment with webpack?

Currently, I'm in the process of developing a node API using express that will generate a PDF utilizing puppeteer for a specified URL. My main concern is how to package my application for production with webpack without having to install puppeteer and express globally on the server where it will be hosted. I also want to avoid running 'npm install' on the server. Is there a solution that would allow me to bundle puppeteer and express together with the JS files for my application?

Answer №1

For those interested in how I tackled this issue, my solution involved utilizing webpack with the following configuration:

import webpack from 'webpack';
import path from 'path';
import nodeExternals from 'webpack-node-externals';

const config: webpack.Configuration = {
    mode: "production",
    entry: "./src/server.ts",
    target: "node",
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: "main.js"
    },
    resolve: {
        extensions: [".ts", ".js"],
      },
    devtool: "source-map",
    module: {
        rules: [
            {
                test: /\.ts$/,
                exclude: /node_modules/,
                use: {
                    loader: "ts-loader",
                    options: {
                        transpileOnly: true
                    }
                }
            }
        ],
    },
    externals: nodeExternals()
}

export default config;

Additionally, on the Continuous Integration server, I executed the following commands:

npm install
webpack
npm ci

One key aspect of this approach is the utilization of npm ci (introduced in npm v6), which eliminates devDependencies from node_modules. Subsequently, I included both the node_modules directory and transpiled js files in the deployment package.

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

fastest-validator: ensure that two fields are not the same

My current validation process involves using fastest-validator. Here is an example of the object being validated: { "_id":"619e00c177f6ea2eccffd09f", "parent_id": "619e00c177f6ea2eccffd09f", } I need to ensu ...

Mastering Typing for Enhanced Order Components using Recompose and TypeScript

I have been working on integrating recompose into my react codebase. As part of this process, I have been experimenting with getting some basic functionality to work. While I have made progress, I am uncertain if I am following the correct approach for usi ...

multiple event listeners combined into a single function

I'm looking to streamline my button handling in JavaScript by creating just one function that can handle all the buttons on my page. Each button will trigger a different action, so I need a way to differentiate between them. I thought of using one eve ...

After the completion of the forEach loop, a response needs to be sent

Currently, I'm grappling with an issue in my NodeJS + Mongoose project where I am attempting to populate an array of objects before sending it to the client. However, I am running into a roadblock as the response always comes back empty due to being s ...

When executing my code to delete a file and remove it from the database, an error message pops up in the console

I've been working on implementing a delete image feature on my website. Although my code successfully deletes the file from the images folder and removes the corresponding record from the database, I encounter an error in the console that prevents me ...

Transform a Typescript type that includes multiple string options into an array containing those options as values

Sending Status: const statusArray = ["confirmed", "pending", "canceled"] Purpose: While the type is automatically generated, I also require it to be in array form. ...

Display a complete inventory of installed typings using the tsd command

How can I display a list of all installed tsd typings in the terminal? Perhaps using the following command: $ tsd list ...

DiscoverField Component with Button Functionality and Checkbox Dilemma

I'm working with Vue 3, Vuetify, and TypeScript for my searchField component. Inside, I have two buttons: FreeItemMaster and PatternMaster. Clicking on these buttons should change their background color and display respective content below them. Howev ...

After constructing my Shopify app, my next step is to guide users to my website by utilizing email and shop URL parameters upon app installation

const Koa = require('koa'); const cors = require('@koa/cors'); var https = require('https'); var http = require('http'); const { default: enforceHttps } = require('koa-sslify'); const next = require('n ...

Tips on converting Sequelize response to an array of strings instead of objects

When utilizing Sequelize on a join query, I am currently facing the issue where the data from the join table is presented as an array of objects instead of strings. The client specifically needs an array of strings. Is it possible and advisable to achieve ...

Solving problems with wildcard routing in express.js

I tried to find a solution online, but the code I found is not working for me. I can't seem to figure out why. Here is my code snippet; app.get('/', (req, res) => { fs.readFile('index.html', function (err, data) { re ...

Encountering "Object is possibly undefined" during NextJS Typescript build process - troubleshooting nextjs build

I recently started working with TypeScript and encountered a problem during nextjs build. While the code runs smoothly in my browser, executing nextjs build results in the error shown below. I attempted various solutions found online but none have worked s ...

Angular mouseenter event delay not functioning after initial trigger

Currently, I am attempting to create a delay for the mouseenter event when the user hovers over a div. The goal is for the video to start playing only after the user has hovered for at least 1 second. Initially, everything works as expected, but after the ...

Upgrading from Angular 4 to Angular 5 may result in the following error message: "Error TS2322: The type 'Promise<void | LineSideInspection>' is not compatible with the type 'Promise<LineSideInspection>'"

I recently updated my Angular project from version 4 to 5.0 and started encountering an error. The code for the entire project (in Angular 4) can be found on github at https://github.com/SudhirSahoo/IQS ERROR in src/app/lineside-inspection/lineside-inspec ...

Unable to establish a connection with the websocket server

I set up a websocket server on my host at <www.example.com>, but for some reason, the client is unable to connect to it. Here is the code : Server.js : const ws = new require('ws'); const wss = new ws.Server({noServer: true}); const htt ...

The Concept of Static Block in TypeScript

For the purpose of testing Network Encoding/Decoding Logic, I have implemented a pair of test cases in both Java and JavaScript. These tests utilize Data Providers which essentially consist of various Constants. In my Java test case, I have a Data Provide ...

Do you need assistance with downloading files and disconnecting from clients?

When looking at the code snippet below: async function (req, res, next) { const fd = await fs.open("myfile.txt") fs.createReadStream(null, { fd, autoClose: false }) .on('error', next) .on('end', () => fs.close(fd)) . ...

The image stored in the Images directory of Node.js is not displaying on the website

Having trouble adding an image to my index.ejs file. The code doesn't seem to be pulling the image from the specified /image folder in the second code block. Any tips on how to resolve this issue would be greatly appreciated! const express = require( ...

Is it false that 0 | "", 2 | {} extends 0 | "", and 0 ? true : false returns false?

0 | "" | {} extends 0 | "" // false 0 | "" | {} extends 0 | {} // true Comparing the union 0 | "" | {} to 0 | "", it seems like the former technically extends from the latter. However, I am puzzled by the ...

Leverage Materialize with TypeScript and Angular

After adding the @types/materialize-css package to my Angular/TypeScript project, I am facing issues with initializing 'M'. Can anyone provide some guidance on how to solve this problem? It's important to note that I prefer not to use angula ...