javascript - 未找到模块:无法解析“child_process”NextJS + Nightmare
问题描述
我正在尝试使用 Next JS + Nightmare Scraper 编译一个简单的示例。但是,当我尝试访问该页面时,我收到以下错误,并且该页面无法加载。
PS C:\Users\lucas\Documents\Projects\ProjectTest\pages\nightmare> npm run dev
Test@1.0.0 dev C:\Users\lucas\Documents\Projects\ProjectTest next dev
准备就绪 - 在 0.0.0.0:3000 上启动服务器,网址:http://localhost:3000
事件 - 编译成功
事件 - 构建页面:/等待 - 编译...错误 - ./node_modules/nightmare/lib/nightmare.js:17:0
未找到模块:无法解析 'child_process' null
在 .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
包.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": {}
}
解决方案
发生错误是因为当nightmare
代码(预计在 Node.js 环境中运行)在客户端上运行时child_process
不存在。您可以添加一个条件以确保它仅在服务器上运行。
if (typeof window === 'undefined') {
const test = require('nightmare');
}
但是,我不确定您为什么要在页面中运行它。它并不意味着在浏览器中运行。
推荐阅读
- java - 如何在 Firebase 数据中进行算术运算?
- javascript - 如何获得发送请求的进度百分比?axios
- javascript - nodejs - 错误:将 ECONNREFUSED 127.0.0.1:80 与 http-proxy 模块连接
- single-sign-on - 从主机访问 windows server 12 r2 adfs
- algorithm - 为什么余弦距离的 KNN 比欧几里得距离快得多?
- java - 使用tomcat和java从eclipse测试数据库连接的以下步骤中缺少什么?我收到404错误
- r - 使用 R 从 Web Feature Service (WFS) 读取信息
- python - 使用for循环(Python)按顺序打印用户输入
- r - rds文件解压后大小不一致
- opencv - 为什么solvePnPRansac返回一个过大的平移向量?