V
V
VarIzo2021-09-15 15:43:49
webpack
VarIzo, 2021-09-15 15:43:49

Why doesn't the script compiled by webpack work?

Good day.
For example, there is a small script that works if you run it directly from the project where it is being developed.
It is built via npm run build does not work giving an error.
Script code

const puppeteer = require('puppeteer');
const browserURL = "http://127.0.0.1:21223"
const url = "https://ya.ru"

const start = async () => {
    const browser = await puppeteer.connect({
        browserURL,
        defaultViewport: {
            width: 1920, height: 1080
        },
    })
    const page = await browser.newPage();
    await await page.goto(url)

}
start()


I develop in php_storm . If you execute it in the terminal, node .\src\r.jsthen the browser opens ya.ru.

If you run npm run build and try to execute the resulting script, it gives an error
(node:3852) UnhandledPromiseRejectionWarning: TypeError: Failed to fetch browser webSocket URL from http://127.0.0.1:21223/json/version: fetch is not a function
    at getWSEndpoint (d:\JS\r.js:8494:30)
    at async Object.connectToBrowser (d:\JS\r.js:8481:31)
    at async start (d:\JS\r.js:33806:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3852) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3852) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


webpack.config

const rConfig = {
    entry: './src/r.js',
    mode: "production",
    target: "node",
    optimization: {
        minimize: false
    },
    output: {
        filename: "r.js",
        path: 'D:\\JS'
    }
}

module.exports = [rConfig]


package.json

{
  "scripts": {
    "build": "webpack"
  },
  "dependencies": {
    "puppeteer": "^10.2.0"
  },
  "devDependencies": {
    "webpack": "^5.52.1",
    "webpack-cli": "^4.8.0"
  }
}


What's happening? Why does not it work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question