简体   繁体   English

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

[英]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.

相关问题 在 Next.js / Webpack 5 中安装 Plaiceholder 导致:找不到模块:无法解析“child_process” - Installing Plaiceholder in Next.js / Webpack 5 causes: Module not found: Can't resolve 'child_process' 找不到模块:使用 Electron React Boilerplate 时无法解析“child_process” - Module not found: Can't resolve 'child_process' while using Electron React Boilerplate 找不到模块:无法解析“child_process” - google-spreadsheet - Module not found: Can't resolve 'child_process' - google-spreadsheet 无法解析模块xmlhttprequest中的“ child_process” - Can't Resolve “child_process” in module xmlhttprequest 无法解析“child_process” - Can't resolve 'child_process' 我正在尝试将 NPM package 作为插件注入到 Next.js 应用程序上,以避免出现“找不到模块:无法解析‘child_process’”错误 - I'm Trying to Inject An NPM package As A Plugin on A Next.js App to Avoid A "Module not found: Can't resolve 'child_process'" Error 错误:无法解析“child_process”和错误:无法解析“fs” - Error: Can't resolve 'child_process' and Error: Can't resolve 'fs' 找不到模块:无法解析“fs”——又一个 NextJS 问题 - Module not found: Can't resolve 'fs' - yet another NextJS problem 在audiosprite中加载错误-找不到模块:“ child_process” - Error loading in audiosprite - Module not found: 'child_process' 找不到模块:无法解析 mp3 Import NextJs Javascript - Module not found: Can't resolve mp3 Import NextJs Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM