V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hgjian
V2EX  ›  Node.js

请问 google 的 puppeteer 模块 和 node.js 自带 的 http、 https 模块 有冲突,怎么解决?

  •  
  •   hgjian · 2019-07-09 14:14:56 +08:00 · 4128 次点击
    这是一个创建于 1743 天前的主题,其中的信息可能已经有所发展或是发生改变。
    测试代码段如下:

    async function puppeteer_promise ( ) {
    ........var puppeteer = require('puppeteer'); /*** 屏蔽测试 ***/
    ........return ;
    }

    async function http_promise ( ) {
    ........var http = require('http');
    ........var https= require('https');

    ........var url = new URL ( "https://www.baidu.com/" ) ;
    ........var http = ( url.protocol == 'https:' ? https : http ) ; // 重新赋值 http ,判断是 http 还是 https ;
    ........console.log( "url.protocol:" + url.protocol ) ;

    ........http.get ( url , function ( res ) { /*** 屏蔽测试 ***/

    ........}).on('error', function (err) {
    ................console.log(err);
    ........});

    ........return ;
    }

    async function test_alldata ( ) {

    ........console.log( "开始测试!" ) ;

    ........await puppeteer_promise();
    ........console.log( "puppeteer 结束!" ) ;

    ........await http_promise();

    ........console.log( "结束测试!" ) ;

    ........return ; // 隐式返回 Promise.resolve( ); 函数 ;

    }

    test_alldata();

    报错信息如下:
    (node:6212) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined
    at urlToOptions (internal/url.js:1259:28)
    at Object.request ( https.js:279:15)
    at Object.request (C:\Users\Claire\Desktop\spider\node_modules\agent-base\patch-core.js:25:22)
    at Object.https.get (C:\Users\Claire\Desktop\spider\node_modules\agent-base\patch-core.js:48:21)
    at http_promise (C:\Users\Claire\Desktop\spider\test.js:17:7)
    at test_alldata (C:\Users\Claire\Desktop\spider\test.js:34:9)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:832:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
    (node:6212) 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(). (rejection id: 2)
    (node:6212) [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.

    Node 10.16
    WIN10_x64 系统

    屏蔽 var puppeteer = require('puppeteer');
    或者 屏蔽
    ........http.get ( url , function ( res ) { /*** 屏蔽测试 ***/

    ........}).on('error', function (err) {
    ................console.log(err);
    ........});
    运行就不会报错。

    请问各位,知道是怎么回事吗?
    有没有什么解决办法啊?
    6 条回复    2019-07-09 20:57:36 +08:00
    azh7138m
        1
    azh7138m  
       2019-07-09 14:36:15 +08:00
    有趣,node v10.15.3 也有这个问题,v12.6.0 没有这个问题。

    另外发帖用 md 就行,不用手动排版,等下班再看下是为啥。
    hgjian
        2
    hgjian  
    OP
       2019-07-09 14:49:33 +08:00 via Android
    @azh7138m 谢谢,我是从 15.3 升级到 16 的。
    azh7138m
        3
    azh7138m  
       2019-07-09 14:56:51 +08:00
    @hgjian 你用 12.6.0 会有这个问题吗?
    hgjian
        4
    hgjian  
    OP
       2019-07-09 14:57:52 +08:00 via Android
    @azh7138m 没有试,晚上试看看
    SakuraKuma
        5
    SakuraKuma  
       2019-07-09 18:32:24 +08:00 via Android
    是 agent base 改变了 http 的行为导致的 …后来 node 修复了
    hgjian
        6
    hgjian  
    OP
       2019-07-09 20:57:36 +08:00
    @SakuraKuma 谢谢解答啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3269 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:41 · PVG 21:41 · LAX 06:41 · JFK 09:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.