如何修复Puppeteer中的“无此类文件或目录”错误

时间:2019-07-04 05:15:39

标签: javascript node.js asynchronous async-await puppeteer

我执行了下一页的代码。但是发生了错误。
我试图更改代码,但没有这样的文件或目录错误无法修复,也无法正常工作。


https://www.npmjs.com/package/puppeteer-to-istanbul

原始代码错误

(node:18592) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open 'C:\Users\username\Desktop\.nyc_output\js\m=WgDvvc,aa,abd,async,dvl,fEVMic,foot,lu,m,mUpTid,mu,sb_wiz,sf,spch,xz7cCd?xjs=s1.js'
    at Object.openSync (fs.js:443:3)
    at Object.writeFileSync (fs.js:1194:35)
    at OutputFiles._parseAndIsolate (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\output-files.js:52:10)
    at new OutputFiles (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\output-files.js:18:10)
    at module.exports (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\output-files.js:62:10)
    at new PuppeteerToIstanbul (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\puppeteer-to-istanbul.js:10:33)
    at module.exports (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\puppeteer-to-istanbul.js:37:10)
    at Object.write (C:\Users\username\node_modules\puppeteer-to-istanbul\index.js:5:17)
    at C:\Users\username\Desktop\original.js:19:7
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:18592) 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: 1)
(node:18592) [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.



将.catch()添加到原始代码中的代码

(async () => {
  const pti = require('puppeteer-to-istanbul')
  const puppeteer = require('puppeteer')
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  // Enable both JavaScript and CSS coverage
  await Promise.all([
    page.coverage.startJSCoverage(),
    page.coverage.startCSSCoverage()
  ]);
  // Navigate to page
  await page.goto('https://www.google.com');
  // Disable both JavaScript and CSS coverage
  const [jsCoverage, cssCoverage] = await Promise.all([
    page.coverage.stopJSCoverage(),
    page.coverage.stopCSSCoverage(),
  ]);
  pti.write(jsCoverage)
  await browser.close()
})().catch(e => console.error(e))

添加.catch()的代码错误

{ Error: ENOENT: no such file or directory, open 'C:\Users\username\Desktop\.nyc_output\js\m=WgDvvc,aa,abd,async,dvl,fEVMic,foot,lu,m,mUpTid,mu,sb_wiz,sf,spch,xz7cCd?xjs=s1.js'
    at Object.openSync (fs.js:443:3)
    at Object.writeFileSync (fs.js:1194:35)
    at OutputFiles._parseAndIsolate (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\output-files.js:52:10)
    at new OutputFiles (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\output-files.js:18:10)
    at module.exports (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\output-files.js:62:10)
    at new PuppeteerToIstanbul (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\puppeteer-to-istanbul.js:10:33)
    at module.exports (C:\Users\username\node_modules\puppeteer-to-istanbul\lib\puppeteer-to-istanbul.js:37:10)
    at Object.write (C:\Users\username\node_modules\puppeteer-to-istanbul\index.js:5:17)
    at C:\Users\username\Desktop\original_add_catch.js:19:7
    at process._tickCallback (internal/process/next_tick.js:68:7)
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path:
   'C:\\Users\\username\\Desktop\\.nyc_output\\js\\m=WgDvvc,aa,abd,async,dvl,fEVMic,foot,lu,m,mUpTid,mu,sb_wiz,sf,spch,xz7cCd?xjs=s1.js' }



以下代码将起作用。

(async () => {
  const pti = require('puppeteer-to-istanbul')
  const puppeteer = require('puppeteer')
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  // Enable both JavaScript and CSS coverage
  await page.coverage.startCSSCoverage()
  // Navigate to page
  await page.goto('https://www.google.com/');
  // Disable both JavaScript and CSS coverage
  const cssCoverage = await page.coverage.stopCSSCoverage();
  pti.write(cssCoverage)
  await browser.close()
})()



环境

  • Windows10
  • node.js v10.16.0

0 个答案:

没有答案