无法在反应应用程序中运行 puppeteer,找不到模块:编译时无法解析“ws" [英] Can't run puppeteer in react app, Module not found: Can't resolve 'ws' when compiling

查看:55
本文介绍了无法在反应应用程序中运行 puppeteer,找不到模块:编译时无法解析“ws"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在我的 React 应用程序中运行 puppeteer.每当我尝试在我的 React 应用程序中运行 puppeteer 时,我都会收到找不到模块:无法解析‘ws’".我已经尝试安装 ws 但仍然会出现相同的错误.

I was wondering if it was possible to run puppeteer in my react app. Whenever I try to run puppeteer in my react app I get "Module not found: Can't resolve 'ws'". I've tried installing ws but will still get the same error.

推荐答案

扩展上述答案.你不能直接在 React 应用上运行 Puppeteer.React 应用程序是一个前端框架,您需要 Puppeteer 才能在节点 js、服务器上运行.这是一个简单的修复"我想比上面的答案多解释一点.

Expanding on the above answer. You cannot directly run Puppeteer on a react app. React app is a frontend framework and you would need Puppeteer to run on a node js, server. It's a simple "fix" and I wanted to explain it a little more than the answer above does.

让他们一起工作的步骤是.

The steps to get them to work together would be.

  1. 设置快速服务器.您可以像这样创建一个快速服务器:单独的目录 reactServer.Npm init 目录和 npm i express.
  1. Setup an express server. You can create an express server like so: Separate directory reactServer. Npm init directory and npm i express.

在你的 express 服务器中你分配了一个路径

In your express server you allocate a path

const express = require('express')
const app = express()
const port = 5000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/my-react-path', (req, res) => {
  // run any scripts you need here
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

然后您的 React 应用程序将像这样与您的服务器交互:

Then your react app will interact with your server like so:

localhost:5000/my-react-path

my-react-path 中,您可以创建一个函数来启动 puppeteer 并在服务器端执行您想要的所有操作,并且它可以将任何结果返回给 React.上面的例子只是为了让你入门,它与 puppeteer 没有任何关系.

In the my-react-path you can create a function that fires up puppeteer and does all the things you want on the server side and it can return whatever results to React. The above example is just to get you started it doesn't have anything related to puppeteer.

这篇关于无法在反应应用程序中运行 puppeteer,找不到模块:编译时无法解析“ws"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆