[英]Module not found: Can't resolve 'child_process' NextJS + Nightmare
I'm trying to compile a simple example by using Next JS + Nightmare Scraper.我正在尝试使用 Next JS + Nightmare Scraper 编译一个简单的示例。 However, when I try to access the page, I'm getting the error below, and the page does not load.
但是,当我尝试访问该页面时,我收到以下错误,并且该页面无法加载。
PS C:\Users\lucas\Documents\Projects\ProjectTest\pages\nightmare> npm run dev
PS C:\Users\lucas\Documents\Projects\ProjectTest\pages\nightmare> npm 运行开发
Test@1.0.0 dev C:\Users\lucas\Documents\Projects\ProjectTest next dev
Test@1.0.0 开发 C:\Users\lucas\Documents\Projects\ProjectTest 下一个开发
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
准备就绪 - 在 0.0.0.0:3000、url: http://localhost:3000 上启动服务器
event - compiled successfully
事件 - 编译成功
event - build page: / wait - compiling... error -./node_modules/nightmare/lib/nightmare.js:17:0
事件-构建页面:/等待-编译...错误-./node_modules/nightmare/lib/nightmare.js:17:0
Module not found: Can't resolve 'child_process' null
找不到模块:无法解析“child_process”null
Could not find files for / in.next/build-manifest.json
找不到 /in.next/build-manifest.json 的文件
Could not find files for / in.next/build-manifest.json
找不到 /in.next/build-manifest.json 的文件
Error from chokidar (C:): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp'
来自 chokidar (C:) 的错误:错误:EBUSY:资源繁忙或锁定,lstat 'C:\DumpStack.log.tmp'
When I run by using node./index.js, it works fine.当我使用 node./index.js 运行时,它工作正常。
The same error occurs either using nightmare or puppeteer.使用 nightmare 或 puppeteer 会发生相同的错误。
index.js index.js
const test = require('nightmare');
function Page() {
return <div>Test 2</div>
}
export default Page
package.json 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": {}
}
The error occurs because when nightmare
code (which expects to run on a Node.js environment) runs on the client child_process
doesn't exist.发生错误是因为当
nightmare
代码(预计在 Node.js 环境中运行)在客户端child_process
上运行时不存在。 You could add a condition to ensure it's only run on the server.您可以添加一个条件以确保它仅在服务器上运行。
if (typeof window === 'undefined') {
const test = require('nightmare');
}
However, I'm unsure why you'd want to run it in a page.但是,我不确定您为什么要在页面中运行它。 It's not meant to run in the browser.
它并不意味着在浏览器中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.