node.js - 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很陌生是什么导致了这个错误?我做错了吗?
解决方案
您正在调用未定义的变量。
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
。
推荐阅读
- java - 当应用程序为 64 位时,Google Play 控制台 64 位错误
- google-tag-manager - 数据层值更改后触发标签
- asp.net - 在 asp.net 核心中是否有 `HttpApplicationtState` 的替代品
- git - 无法在 centos 中连接到 azure git
- django-rest-framework - DRF 序列化程序接受序列化程序字符字段的 int 值,而不是为未匹配的值引发错误
- mysql - sql:计数案例(加权)
- mysql - 使用 MySQL 用户定义变量和 CakePHP3 查询选择
- flutter - 如何使导航抽屉出现在应用栏/工具栏的底部
- javascript - 更改在 javascript 中的函数内调用的函数
- excel - Display the address of the last non-empty cell in a column (no matter if column contains values or text)