首页 > 解决方案 > 运行 build-installer (ElectronJS) 后 Puppeteer 不工作

问题描述

我创建了一个网络抓取应用程序,在我运行 npm run build-installer 命令之前它运行良好。在我打开“dist”文件夹中的可执行文件后,它给了我这个错误:

internal/child_process.js:394 Uncaught (in promise) Error: spawn ENOTDIR
    at ChildProcess.spawn (internal/child_process.js:394)
    at Object.spawn (child_process.js:549)
    at BrowserRunner.start (/Users/x/Desktop…BrowserRunner.js:80)
    at ChromeLauncher.launch (/Users/x/Desktop…node/Launcher.js:88)
    at async checkPrice (<anonymous>:21:21)
errnoException  @   internal/errors.js:474
ChildProcess.spawn  @   internal/child_process.js:394
spawn   @   child_process.js:549
start   @   /Users/x/Desktop…BrowserRunner.js:80
launch  @   /Users/x/Desktop…node/Launcher.js:88
async function (async)      
checkPrice  @   VM69:21
onclick @   VM229 index.html:1

这是我的 price.js 文件


    const puppeteer = require('puppeteer')
    const loader = document.getElementById('loader')

    const flrPrice = document.getElementById('flrPrice')
    const datetime = document.getElementById('datetime')
    
    async function checkPrice(url){
      console.log("starting...")
      const puppeteer = require('puppeteer')
        const loader = document.getElementById('loader')

        const flrPrice = document.getElementById('flrPrice')
        const datetime = document.getElementById('datetime')
        console.log("read req.")
    flrPrice.style.visibility = 'hidden'
    datetime.style.visibility = 'hidden'
    loader.style.visibility = 'visible'
    

    const browser = await puppeteer.launch()
    const page = await browser.newPage()
    await page.goto(url)
    const [el] = await page.$x('//*[@id="__next"]/div/div[1]/main/div/div[7]/div/div/section/div[2]/div[1]/div[1]/div[3]/span/text()[2]');
    const txt = await el.getProperty('textContent');
    const price = await txt.jsonValue();
    
    console.log("completed task")


    loader.style.visibility = 'hidden'
    flrPrice.style.visibility = 'visible'
    datetime.style.visibility = 'visible'


    console.log("show")

    function formatDate(date) {
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear() + "  " + strTime;
      }
      var d = new Date();
      var displayTime = formatDate(d);
      document.getElementById("flrPrice").innerHTML = 'FLR Price: $' + price;
      document.getElementById("datetime").innerHTML = 'As of ' + displayTime;

    browser.close()
    }
    

最后是 price.html 文件:

<style>

.loader,
.loader:before,
.loader:after {
  border-radius: 50%;
  width: 1.5em;
  height: 1.5em;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation: load7 1.8s infinite ease-in-out;
  animation: load7 1.8s infinite ease-in-out;
}
.loader {
  visibility: hidden;
  color: #ffffff;
  font-size: 10px;
  margin: 40px auto;
  position: relative;
  text-indent: -9999em;
  -webkit-transform: translateZ(0);
  -ms-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-animation-delay: -0.16s;
  animation-delay: -0.16s;
}
.loader:before,
.loader:after {
  content: '';
  position: absolute;
  top: 0;
}
.loader:before {
  left: -3.5em;
  -webkit-animation-delay: -0.32s;
  animation-delay: -0.32s;
}
.loader:after {
  left: 3.5em;
}
@-webkit-keyframes load7 {
  0%,
  80%,
  100% {
    box-shadow: 0 2.5em 0 -1.3em;
  }
  40% {
    box-shadow: 0 2.5em 0 0;
  }
}
@keyframes load7 {
  0%,
  80%,
  100% {
    box-shadow: 0 2.5em 0 -1.3em;
  }
  40% {
    box-shadow: 0 2.5em 0 0;
  }
}



.kokuban-t1 {
    text-align: center;
    position: relative;
    margin: 2em auto; 
    padding: 3.2em 1em 1em;

    height: 80%;
    width: 100%; 
    color: #fff; /* word color */

   }

   .title-t1 {
    text-align: center;
    font-weight: bold;
    font-size: 2em;
    color: #f5632a; /* タイトル色 */
   }

   .title-t1-time {
    text-align: center;
    font-weight: bold;
    font-size: 1em;
    color: #f5632a; /* タイトル色 */
   }

   .title-t1-price {
    text-align: center;
    font-weight: bold;
    font-size: 2em;
    color: #78f56d; /* タイトル色 */
   }
   
   .marker {
   color: #ff0000;
   background: rgba(204, 204, 204, 0);
   font-weight: bold; 
   }
   
   
   .button {
     background-color: #f5632a;
     border: none;
     border-radius: 40px;
     color: white;
     padding: 10px;
     text-align: center;
     text-decoration: none;
     display: inline-block;
     font-size: 15px;
     margin: 4px 2px;
     cursor: pointer;
   }
   .button:hover {
     background-color: #a34a27;
   }
   
   .button:active {
     background-color: #633a0a;
   }
   

</style>
<head>

  
  <script type="text/javascript" src="./price.js"></script>
</head>
<body>
  <div class="kokuban-t1"><span class="title-t1">Flare Price</span> 
    <br>
    <br>
    <br>
    <br>
    
    <div id="flrPrice" class="title-t1-price"></div>
<br>
    <div id="datetime" class="title-t1-time"></div>

 <div id="loader" class="loader">Loading...</div>

<br>


 <form id="form" onsubmit="return false;">
   <br>

<input id="submitbutton"; value="Check Price"; style=top:50%;width:20%; class=button type="submit" onclick="checkPrice('https://www.livecoinwatch.com/price/Spark-FLR')">

 </form>





 </div>
</body>


当我在 npm run start 中运行该代码时,该代码有效,但在我使用 npm run build-installer 编译它之后,会发生错误。

标签: javascript

解决方案


推荐阅读