I have this code here in VSCode:
const form = document.querySelector('form');
The error I am getting is:
C:\Program Files\nodejs\node.exe boot.js
helloworld
c:\Users\frase\OneDrive\Desktop\html\boot.js:2
const form = document.querySelector('form');
^
ReferenceError: document is not defined
at Object.<anonymous> (c:\Users\frase\OneDrive\Desktop\html\boot.js:2:14)
at Module._compile (internal/modules/cjs/loader.js:1157:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
at Module.load (internal/modules/cjs/loader.js:1001:32)
at Function.Module._load (internal/modules/cjs/loader.js:900:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
Why is the document not working? The file name is referenced with the html file as <script src="boot.js"></script>
I have tried putting the script in the header and body tags.
document is available only when you are running javascript in a browser. It is not available by default.
Related
I am using Node.js Discord.js-commando, I try to run my code but this comes up please tell me if you are having the same error or have a fix. The code under is from the index.js for your help. Please reply to this message and tell me if you know ut
C:\Users\james\OneDrive\Desktop\Discover Now\node_modules\discord.js-commando\src\registry.js:129
throw new Error(`A command with the name/alias "${command.name}" is already registered.`);
^
Error: A command with the name/alias "help" is already registered.
at CommandoRegistry.registerCommand (C:\Users\james\OneDrive\Desktop\Discover Now\node_modules\discord.js-commando\src\registry.js:129:10)
at CommandoRegistry.registerCommands (C:\Users\james\OneDrive\Desktop\Discover Now\node_modules\discord.js-commando\src\registry.js:176:9)
at CommandoRegistry.registerCommandsIn (C:\Users\james\OneDrive\Desktop\Discover Now\node_modules\discord.js-commando\src\registry.js:200:15)
at Object.<anonymous> (C:\Users\james\OneDrive\Desktop\Discover Now\index.js:67:4)
at Module._compile (node:internal/modules/cjs/loader:1102:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
at Module.load (node:internal/modules/cjs/loader:967:32)
at Function.Module._load (node:internal/modules/cjs/loader:807:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
This is my code/ command handler in my index.js I have 5 categories each with their own name. Please rply with a a answer if u have one
client.registry
.registerDefaultTypes()
.registerGroups([
["fun", "Fun Commands"],
["moderation", "Moderation Commands"],
["special", "Special Commands"],
["misc", "Misc Commands"],
["music", "Music Commands"]
])
.registerDefaultGroups()
.registerDefaultCommands()
.registerCommandsIn(path.join(__dirname, "commands"));
I'm writing a GitHub action that receives a mandatory input field named file using #actions/core library.
const core = require("#actions/core");
async function run() {
try {
let file = core.getInput('file', {required: true});
// rest of my action ...
I'm able to run it locally and it fails as expected as-is (no input provided).
Is there a built-in way to provide inputs (similar to env-vars) so I could run and test it locally?
Error: Input required and not supplied: file
at Object.getInput (.../node_modules/#actions/core/lib/core.js:78:15)
at run (.../src/main.js:6:25)
at Object.<anonymous> (.../src/main.js:40:5)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
::error::Input required and not supplied: file
If you look at the sources of getInput, you can see that it is using environment variables:
const val: string =
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''
Knowing this, you can just set this environment variable:
const setInput = (name,value)=>
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`]=value;
JavaScript code:
node
const Web3 = require('web3')
const web3 = new Web3('network-link')
console.log(web3.eth.accounts.create())
Now I saved this file in the desktop: file.js
Command prompt code:
path/path/path/path
cd Desktop
path/path/path/path/Desktop
node file
This outputs:
ReferenceError: node is not defined
at Object.<anonymous> (C:\Users\HP\Desktop\file.js:1:1)
[90m at Module._compile (internal/modules/cjs/loader.js:956:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:812:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:724:14)[39m
[90m at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)[39m
[90m at internal/main/run_main_module.js:17:11[39m
You need to remove node in first line of your code, after refactoring it will be as
const Web3 = require('web3')
const web3 = new Web3('network-link')
console.log(web3.eth.accounts.create())
Because you neither declared node as a variable nor as a function. So when file gets run then node is undefined.
I am working through Brad Dayley's Node.js, MongoDB and Angularjs book and I'm stuck on one of his exercises (Listing 4.4). I have a simple script emitterListener.js that is as follows the script is designed to makes checks on an account.
var events = require('events');
function Account() {
this.balance = 0;
events.EventEmitter.call(this);
this.deposit = function(amount) {
this.balance += amount;
this.emit('balanceChanged');
};
this.withdraw = function(amount) {
this.balance -= amount;
this.emit('balanceChanged');
};
}
Account.prototype._proto_ = events.EventEmitter.prototype;
function displayBalance() {
console.log("Account balance: $%d", this.balance);
}
function checkOverdraw() {
if (this.balance < 0) {
console.log("Account Overdrawn!!!!!");
}
}
function checkGoal(acc, goal) {
if (acc.balance > goal) {
console.log("Goal Achieved!!!!!");
}
}
var account = new Account();
account.on("balanceChanged", displayBalance);
account.on("balanceChanged", checkOverdraw);
account.on("balanceChanged", function() {
checkGoal(this, 1000);
});
account.deposit(220);
account.deposit(320);
account.deposit(600);
account.withdraw(1200);
When I run this I get the error.
TypeError: Object #<Account> has no method 'on'
at Object.<anonymous> (/Users/506132/web/emitterListener.js:35:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
From my limited understanding after researching I think this means that the "on" module is not being loaded. I found a solution that suggested something similar to adding this to line 1
var events = require('events').on;
which then results in the error.
TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Following the logic from the first fix I tried implementing the same fix but with EventEmitter
var events = require('events').EventEmitter;
Hooray it looks like it worked.... or not......and now I get this error
TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:17:48)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
I tried adding the below code thinking why not?
var events = require('events').prototype;
and it just brings me back to the same error from before
TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
What am I doing wrong here? How should I go about debugging this and where should I look? Thanks in advance for helping a newbie out.
Cheers.
process.EventEmitter is deprecated, use require('events') instead.
Search through all your reference files (should be in multiple files) for:
var EventEmitter = process.EventEmitter
An where ever it exists, change that line to this:
var EventEmitter = require('events')
I'll post it as an answer so this question won't get marked as unanswered.
You should change:
Account.prototype.__proto__ = events.EventEmitter.prototype;
to:
Account.prototype = Object.create(events.EventEmitter.prototype);
var EventEmitter = process.EventEmitter
is deprecated in high version of node, use var EventEmitter = require('events') instead
This compilation error because of versions conflicts of npm and node. I had the same issue in my project. I resolved after install the same compatible versions of npm and node as on another machine.
Check:
nmp -v
and
node -v.
All the confusion with the actual example is the first line of code.
var events = require('events');
After Node v6.1.0 this line of code will store EventEmmiter object inside events variable, so there is no need to access it explicitly later.
Solution is very simple, just change
events.EventEmitter.call(this);
to:
events.call(this)
And change
Account.prototype._proto_ = events.EventEmitter.prototype;
to:
Account.prototype._proto_ = events.prototype;
I had the same problem a few minutes ago, I decided to downgrade node to version 6.x, then it worked
I have an issue with exporting a module in Node.js.
Here is a part of my code:
var zmq = require('zmq');
var module = require('module');
var DeviceRequester = function(port, name)
{
...
};
var SMValueGetter = function(socket)
{
...
};
module.exports.DeviceRequester = DeviceRequester;
module.exports.SMValueGetter = SMValueGetter;
When I use it I get the following error:
module.exports.DeviceRequester = DeviceRequester;
^
TypeError: Cannot set property 'DeviceRequester' of undefined
at Object.<anonymous> (<PROJECT_PATH>/node_modules/lse/lib/lse.js:168:32)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (<PROJECT_PATH>/server.js:6:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
I have no idea why it occurs. I've red the Node.js documentation and some tutorials about exporting modules and all of them do it that way. Am I overlooking something?
Give more attentions at errors
TypeError: Cannot set property 'DeviceRequester' of undefined
at line var module=require("module"); you are overwriting the default module variable.
simple try to change at something that makes more sense, such as
var MyModule=require(..path..)