Module not found: Can't resolve 'child_process' NextJS + Nightmare

3k Views Asked by At

I'm trying to compile a simple example by using 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

[email protected] dev C:\Users\lucas\Documents\Projects\ProjectTest next dev

ready - started server on 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

Module not found: Can't resolve 'child_process' null

Could not find files for / in .next/build-manifest.json

Could not find files for / in .next/build-manifest.json

Error from chokidar (C:): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp'

When I run by using node ./index.js, it works fine.

The same error occurs either using nightmare or 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

There are 1 best solutions below

0
juliomalves On

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. 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.