在客户端JS网页上使用require()(browserify出现'promisify is a function'错误) [英] Use require() on client-side JS webpage ('promisify is not a function' error with browserify)

查看:81
本文介绍了在客户端JS网页上使用require()(browserify出现'promisify is a function'错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个使用Puppeteer(用于Web抓取的NodeJS库)的简单网页.我无法运行我的页面,因为我从index.html链接到的main.js文件通过require()使用Puppeteer并在加载页面时给我一个错误,因为require()无法在客户端上使用,一侧.

I'm trying to create a simple webpage that uses Puppeteer (a NodeJS library for web scraping). I can't run my page because my main.js file that I link to from my index.html uses Puppeteer through require() and gives me an error when the page is loaded since require() can't be used on client-side.

我试图通过使用Browserify将我的main.js文件与Puppeteer依赖项捆绑在一起来解决此问题,但是当使用来自Browserify的新创建的bundle.js运行页面时,在浏览器控制台中出现以下错误:

I tried to solve this by using Browserify to bundle my main.js file with the Puppeteer dependency but I get the following error in the browser console when running the page with the newly created bundle.js from Browserify:

Uncaught TypeError: promisify is not a function
at Object.37.debug (bundle.js:2470)
at o (bundle.js:1)
at bundle.js:1
at Object.109.../common/Debug.js (bundle.js:14756)
at o (bundle.js:1)
at bundle.js:1
at Object.<anonymous> (bundle.js:15470)
at Object.111.../common/Browser.js (bundle.js:16004)
at o (bundle.js:1)
at bundle.js:1

我尝试安装promisify和util.promisify,但无法解决问题.

I tried installing promisify and util.promisify but it didn't solve the issue.

这些文件是超级基本的文件,用于测试其是否正常工作,看起来像这样:

The files are super basic to test if it works and look like this:

index.html

<!DOCTYPE html> <html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css">
<title>Document</title> </head> <body>
<h1>My Page</h1>
<script src="bundle.js"></script> </body> </html>

main.js

const puppeteer = require('puppeteer'); 
alert('Hello');

用于浏览器的命令

browserify main.js -o bundle.js

browserify main.js -o bundle.js

有什么想法为什么不起作用?

Any ideas why it doens't work?

推荐答案

Browserify解决了浏览器不支持 require 的问题.

Browserify solves the problem of require not being supported by browsers.

它还允许您使用npm和yarn之类的工具来管理JS软件包,因为它可以像Node.js一样使用 require 来解析它们.

It also allows you to use tools like npm and yarn to manage JS packages since it can resolve them using require like Node.js can.

它不能解决模块问题,例如 puppeteer ,具体取决于Node.js提供的API,而不是浏览器提供的API.您无法在客户端运行Puppeteer.

It doesn’t solve the problem of modules, like puppeteer, depending on APIs that are provided by Node.js and not by browsers. You can’t run Puppeteer client-side.

这篇关于在客户端JS网页上使用require()(browserify出现'promisify is a function'错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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