![](/img/trans.png)
[英]Installing Plaiceholder in Next.js / Webpack 5 causes: Module not found: Can't resolve 'child_process'
[英]Module not found: Can't resolve 'child_process' NextJS + Nightmare
我正在嘗試使用 Next JS + Nightmare Scraper 編譯一個簡單的示例。 但是,當我嘗試訪問該頁面時,我收到以下錯誤,並且該頁面無法加載。
PS C:\Users\lucas\Documents\Projects\ProjectTest\pages\nightmare> npm 運行開發
Test@1.0.0 開發 C:\Users\lucas\Documents\Projects\ProjectTest 下一個開發
准備就緒 - 在 0.0.0.0:3000、url: http://localhost:3000 上啟動服務器
事件 - 編譯成功
事件-構建頁面:/等待-編譯...錯誤-./node_modules/nightmare/lib/nightmare.js:17:0
找不到模塊:無法解析“child_process”null
找不到 /in.next/build-manifest.json 的文件
找不到 /in.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": {}
}
發生錯誤是因為當nightmare
代碼(預計在 Node.js 環境中運行)在客戶端child_process
上運行時不存在。 您可以添加一個條件以確保它僅在服務器上運行。
if (typeof window === 'undefined') {
const test = require('nightmare');
}
但是,我不確定您為什么要在頁面中運行它。 它並不意味着在瀏覽器中運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.