首页 > 解决方案 > Nodejs,Electron噩梦安装时没有定义?

问题描述

我已经通过 NPM 安装了 Nightmare,这是我的代码:

var jquery = require('jquery')
var nightmare = require('nightmare')
var nightmare = Nightmare({ show: true });

$( "#test" ).addEventListener('click',() => {
    nightmare
        .goto('http://akhiljose.me/master/paste/')
        .type('.form-control', 'Test')
        .type('input[type=test]', 'nightmare_test')
        .click('input[type=submit]')
        .wait(7000)
        .evaluate(function () {
        return document.querySelector('pre').innerText;
        })
        .end()
        .then(function (result) {
            console.log(result);
        })
        .cat(function (error) {
        console.error('Search failed:', error);
        })});

但是控制台日志:

C:\Users\ninja_000\Desktop\clu-gen\index.js:3 Uncaught ReferenceError: Nightmare is not defined
    at Object.<anonymous> (C:\Users\ninja_000\Desktop\clu-gen\index.js:3:17)
    at Object.<anonymous> (C:\Users\ninja_000\Desktop\clu-gen\index.js:22:3)
    at Module._compile (module.js:642:30)
    at Object.Module._extensions..js (module.js:653:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:504:12)
    at Function.Module._load (module.js:496:3)
    at Module.require (module.js:586:17)
    at require (internal/module.js:11:18)
    at file:///C:/Users/ninja_000/Desktop/clu-gen/index.html:12:5

我对nodejs很陌生是什么导致了这个错误?我做错了吗?

标签: node.jselectron

解决方案


您正在调用未定义的变量。

var jquery = require('jquery')
var nightmare = require('nightmare')
var nightmare = Nightmare({ show: true });

第二行声明了一个变量nightmare,但下一行您正在调用Nightmare. 将第二行设为大写。

var jquery = require('jquery')
var Nightmare = require('nightmare')
var nightmare = Nightmare({ show: true });

您可以从堆栈跟踪的第二行看到:

at Object.<anonymous> (C:\Users\ninja_000\Desktop\clu-gen\index.js:3:17)

3:17 行,有一个 uncaught ReferenceError: Nightmare。这是有道理的,因为Nightmare没有定义,所以 nodejs 找不到它。堆栈跟踪中的行号有助于查明代码中发生错误的位置。您还可以使用 linter,它会在尝试使用未定义的变量时显示错误。类似的东西eslint


推荐阅读