找不到模块:无法解析“child_process”NextJS + Nightmare

时间:2021-02-14 17:31:39

标签: javascript web-scraping next.js puppeteer nightmare

我正在尝试使用 Next JS + Nightmare Scraper 编译一个简单的示例。但是,当我尝试访问该页面时,出现以下错误,并且该页面无法加载。

<块引用>

PS C:\Users\lucas\Documents\Projects\ProjectTest\pages\nightmare> npm 运行开发

Test@1.0.0 dev C:\Users\lucas\Documents\Projects\ProjectTest 下一个开发

就绪 - 在 0.0.0.0:3000 上启动服务器,网址:http://localhost:3000

事件 - 编译成功

事件-构建页面:/等待-编译...错误- ./node_modules/nightmare/lib/nightmare.js:17:0

未找到模块:无法解析“child_process”为空

在 .next/build-manifest.json 中找不到 / 的文件

在 .next/build-manifest.json 中找不到 / 的文件

来自 chokidar (C:) 的错误:错误:EBUSY:资源繁忙或锁定, lstat 'C:\DumpStack.log.tmp'

当我使用 node ./index.js 运行时,它工作正常。

使用 nightmare 或 puppeteer 会出现同样的错误。

index.js

const test = require('nightmare');

function Page() {

    return <div>Test 2</div>
}

export default Page

package.json

{
  "name": "Test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "next dev"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nightmare": "^3.0.2",
    "next": "^10.0.6",
    "puppeteer": "^7.1.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1"
  },
  "devDependencies": {}
}

1 个答案:

答案 0 :(得分:-1)

发生错误是因为当 nightmare 代码(期望在 Node.js 环境中运行)在客户端上运行时,child_process 不存在。您可以添加一个条件以确保它仅在服务器上运行。

if (typeof window === 'undefined') {
    const test = require('nightmare');
}

但是,我不确定您为什么要在页面中运行它。它并不打算在浏览器中运行。

相关问题