Open zer0grand opened 3 years ago
How are you importing the package? What language are you using (JS/TS)?
JS. I've never used TS before- is there something obvious I'm missing because I'm using JS?
No, TS and JS are similar in usage, though TS uses import export
and Node.js uses require
usually.
If you're following the README, then it's a bit off for JS when using non-modules:
const Rcon = require('srcds-rcon'); // Use require instead of import when using native Node.js
const server = new Rcon({ host: '127.0.0.1', port: 25010 });
try {
await server.authenticate('your_rcon_password');
console.log('authenticated');
let status = await server.execute('status'); // You can read `status` reponse
server.execute('mp_autokick 0'); // no need to read the response
} catch(e) {
console.error(e);
}
Also, you should ensure you're on the latest version of srcds-rcon, anything under 2.0.1
will give you the Rcon is not a constructor.
as mentioned in this pull request
Yeah I thought the import
part was weird so I did try replacing it with require
, though still had the same error. I just checked my version by doing npm list srcds-rcon
and I get srcds-rcon@2.2.1
so I believe I have a working version. Anything else I could be doing wrong? Thanks for the quick responses btw!
Oh, you're using srcds-rcon
instead of rcon-srcds
. You're just in the wrong library!
If you want to continue using that library, go to this URL or continue to use this library by using these commands:
npm uninstall srcds-rcon
npm install rcon-srcds
A bit of a confusing naming scheme, but this'll fix your issue!
LOL whooooops. Well I just switched it but I somehow still am back where I started. I am still getting the exact same error. I'm using the code you posted above, with the exception of using the new naming rcon-srcds
. (the naming ended up being backwards in the code example above as well)
const Rcon = require('rcon-srcds'); // Use require instead of import when using native Node.js
const server = new Rcon({ host: '127.0.0.1', port: 25010 });
gives me
C:\>node test.js
C:\test.js:2
const server = new Rcon({ host: '127.0.0.1', port: 25010 });
^
TypeError: Rcon is not a constructor
at Object.<anonymous> (C:\test.js:2:16)
←[90m at Module._compile (node:internal/modules/cjs/loader:1108:14)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:973:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:813:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m
Alrighty, I'll have to try this tomorrow then.
I'll get back if I find a solution.
The problem:
// rcon.js, line 238
// Cannot be read by the module system
exports.default = RCON;
When converting this to use modules
, I can finally read my server's status:
// rcon.js, line 238
module.exports = RCON;
I have recreated this issue by doing the following:
// app.js
const Rcon = require("rcon-srcds");
const server = new Rcon({
host: "my.server.here"
});
(async () => {
await server.authenticate("password");
console.log(await server.execute("status"));
return await server.disconnect();
})();
/*
Expects:
hostname: my_server_here
version : ...
Gets:
const server = new Rcon({
^
TypeError: Rcon is not a constructor
*/
Doing the same thing in TypeScript will not throw this error, since it the resulting code is compiled. Tested with same example as above, and it works as expected.
You can either use TypeScript, which is a superset of JavaScript, or modify the lines as decribed below.
To do this, you rename your app.js
file to app.ts
and use tsc app.js
to compile the TypeScript file to JavaScript, and run that compiled file, which will work from my testing. This is a bit much to do if you need to convert a whole project to TypeScript, so a better solution is below.
The solution: https://stackoverflow.com/questions/40294870/module-exports-vs-export-default-in-node-js-and-es6
Because this library is written in ES6, the tsc compiler converts the export default RCON
to exports.default = RCON
. This in turns makes the importing when using commonjs a bit different:
// Wrong
const Rcon = require('rcon-srcds');
// Right
const Rcon = require('rcon-srcds').default;
This isn't necessarily a bug with the library, just how tsc compiles the source code.
Can this issue be closed?
Not sure if I'm doing something wrong... but copying and pasting either example and running it gives me an error saying Rcon is not a constructor.