I have a working discord bot, but I would like to compile it into a .exe file. How to do that?
I tried to use pkg and nexe, but both did not make it correctly. Nexe did not even start giving me some strange errors that I did not manage to fix after reading docs and some discussions on the Internet.
Error: https://github.com/nexe/nexe/releases/download/v3.3.3/windows-x64-14.8.0 is not available, create it using the --build flag and Error: vcbuild.bat nosign release x64 exited with code: 1.
The pkg worked better, I got and exe file, but it did not seem to work:
I have this code, where bot is my discord client.
bot.on('ready', () =>
{
console.log(bot.guilds.cache.toJSON());
bot.guilds.cache.forEach((gui) =>
{
console.log(gui.id + ' ' + gui.name + ' ' + gui.memberCount);
});
console.log('Bot started!');
});
It gives one server when I start it using node index.js, but does not give any if I execute the exe file created, though the bot becomes online.
My package.json looks like this:
{
"name": "sth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"pkg": {
"assets": [ "jsons/**/*", "config.json" ]
},
"bin": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^12.4.1"
}
}
What am I doing wrong? Why does the bot start, but does not seem to be able to do anything? What have I missed?
i don't know of an .exe file code you can use BUT you can use a .bat file and put it on desktop and create shortcuts or put it on shell:startup in that you will want to write this
#echo off
node "js file path"
Related
I am new to Node.js. I want to create mysql connection and insert data into the database and access it later. I installed, node.js, then using npm installed 'mysql' and 'faker'.
Using node.js I successfully connected to mysql, created database and tables(code in mysql1.js in the folder NODE-JS).
Then I went ahead to create fake data (code in fake_data.js) to insert into the table.
screenshot of vscode
var faker = require("faker");
let first_name = faker.name.firstName();
let last_name = faker.name.lastName();
console.log(Employee: ${prefix} ${first_name} ${last_name} ${suffix});
But this time I got error
code: 'MODULE_NOT_FOUND',
path: 'D:\\DATABASES\\NODE-JS\\node_modules\\faker\\package.json',
requestPath: 'faker'
package.json file contains the following dependencies
{
"dependencies": {
"faker": "^6.6.6",
"mysql": "^2.18.1"
},
"name": "node-js",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
I tried,
var faker = require("./faker");
even then it throws error
please help me to rectify the problem. thanks
The owner of faker deleted it from GitHub at v6.6.6
You can see that here:
https://github.com/marak/Faker.js/
So it is up to the community to complete it
The new repository:
https://github.com/faker-js/faker
This is the official, stable fork of Faker.
Installation
Please replace your faker dependency with #faker-js/faker.
npm install #faker-js/faker --save-dev
Node.js
const { faker } = require('#faker-js/faker');
Hi I am starting with Node Js and tried a code as per the documentation over here. I did everything accordingly but I am getting below error.
const Bumblebee = require('bumblebee-hotword');
^
SyntaxError: Identifier 'Bumblebee' has already been declared
at Loader.moduleStrategy (node:internal/modules/esm/translators:147:18)
at async link (node:internal/modules/esm/module_job:48:21)
Here is the index.js code
import Bumblebee from "bumblebee-hotword";
const Bumblebee = require('bumblebee-hotword');
let bumblebee = new Bumblebee();
// set path to worker files
bumblebee.setWorkersPath('/bumblebee-workers');
// add hotword
bumblebee.addHotword('jarvis');
// set sensitivity from 0.0 to 1.0
bumblebee.setSensitivity(1.0);
bumblebee.on('hotword', function(hotword) {
// YOUR CODE HERE
console.log('hotword detected:', hotword);
});
bumblebee.start();
And here is the package.json
{
"name": "Hotword_template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"bumblebee-hotword": "^0.2.1"
}
}
Also the required directory "bumblebee-workers" is in the same directory.
I don't know where I am doing wrong, any help is highly appreciated!!
Look at these two lines, you are importing and using constant variable of same name.
import Bumblebee from "bumblebee-hotword";
const Bumblebee = require('bumblebee-hotword');
Changing the name of const variable can solve your problem.
I try to build my first npm package, but now I got stuck.
I want to load from my package a config file that is located in the root of my test-project
I used "npm link my-package" to install it locale for testing (if this important)
This is my folder structure
- my-package
- test-project
- node-modules
- my-package (npm link)
- config.json
The package is a vuejs app that should start a server and serve a page
I run this script from my test-project
"scripts": {
"generatePage": "npm explore my-package -- npm run serve"
},
script from my package
"scripts": {
"serve": "node readFile.js & vue-cli-service serve"
},
my-package/readFile.js
file_path = path.join(process.cwd(), 'config.json')
console.log('file_path', file_path)
If I'm running my script I get this path /Users/name/work/my-package/config.json but I need /Users/name/work/test-project/config.json
How do I get the correct path?
When you write a library that will be used by another code base you cannot be sure where exactly NPM or yarn (or whatever package manager you choose) will install your library. It may be directly in node_modules but it may be nested depending upon the use case. However, as indicated by the stackoverflow answer here the process.main.filename variable will tell us exactly where the main process that is calling us lives. We can use this value to determine where the config file you want to read exists.
Here is some example code:
The config-lib library reads a configuration.
index.js file:
const fs = require("fs");
const path = require("path");
const promisify = require("util").promisify;
const readFilep = promisify(fs.readFile);
module.exports = async function loadConfig(filename = "config.json") {
const configPath = path.resolve(
path.dirname(require.main.filename),
filename
);
console.log("(load-config lib) Config Path: " + configPath);
try {
const data = await readFilep(configPath, "utf8");
return JSON.parse(data);
} catch (err) {
console.log(err);
}
};
This library has a simple package.json
{
"name": "config-lib",
"version": "1.0.0",
"description": "I read a config",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Some Cool Guy <somecoolguy#example.com>",
"license": "MIT"
}
Now the project itself requires this library. Here is the projects package.json
{
"name": "project",
"version": "1.0.0",
"description": "I am the project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Some Other Cool Guy <someoneelse#example.com>",
"license": "MIT",
"dependencies": {
"config-lib": "file:../lib"
}
}
(The config-lib library in this example was loaded from a local folder.)
The config-lib library requires me, in my project, to create a configuration file called config.json (BTW... name it something more specific to your library as to not collide with a local project configuration if they have one).
Here is an example config.json:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Finally, in my project I can use the config-lib library to read my configuration file:
const configLib = require("config-lib");
(async () => {
console.log("Reading config");
const data = await configLib();
console.log("Here is my config: " + JSON.stringify(data));
console.log("key1 value: " + data.key1);
})();
Hi everyone I'm beginner in nodejs .I want to test some basic functionality in my code.I m using JEST testing framework.In the command prompt I used npm test I could not test my code it shows
npm ERR! Test failed. Can anyone solve this issue? Thanks in advance...
lib.test.js:
const lib=require('../lib.js');
describe('absolute',()=>{
it('It should return positive number if given number is positive',()=>{
const result=lib.absolute(1);
expect(result).toBe(1);
});
it('It should return positive number if given number is negative',()=>{
const result=lib.absolute(-1);
expect(result).toBe(1);
})
it('It should return zero if given number is zero',()=>{
const result=lib.absolute(0);
expect(result).toBe(1);
});
});
lib.js:
// Testing numbers
module.exports.absolute = function(number) {
if (number > 0) return number;
if (number < 0) return -number;
return 0;
}
package.json:
{
"name": "testing-demo",
"version": "1.0.0",
"description": "",
"main": "db.js",
"directories": {
"test": "tests"
},
"dependencies": {},
"devDependencies": {
"jest": "^22.2.2"
},
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC"
}
I'm guessing the error you are seeing is
SecurityError: localStorage is not available for opaque origins
The default test environment for Jest is a browser-like environment provided by jsdom, which is required for testing code like React applications that are designed to run in the browser.
If you are going to use jsdom as the test environment then you need to set testURL in the Jest configuration, otherwise you will get the error seen above.
If your code is designed to run on node and you don't need a browser-like environment then you can set your test environment to be node.
The easiest way to do that in your case is to pass it as a command line argument to jest in your package.json:
"scripts": {
"test": "jest --env=node"
},
I wanted to build a simple .net core MVC app with a single class library for data access, to get an idea of what it takes to build/deploy/run Core on Linux. Not nearly as simple as I had hoped!
EDIT: Here's a copy of the solution, if anyone is interested:
https://s3.amazonaws.com/kilonova-public/code/CoreCrossPlatform.zip
I threw together a VirtualBox VM w/ Ubuntu Server 16.04 and installed dotnet core per these instructions:
https://www.microsoft.com/net/core#ubuntu
I installed all the latest, necessary bits on the host (Win10) for VS 2015 and created a solution with an MVC app and single class library called "DataAccess". It's EF Core talking to MySQL using their latest core provider. It all runs flawlessly on the Win10 host when I run/debug it. Pulls up data and looks great.
"dotnet --version" on both the host and the VM gives me:
1.0.0-preview2-003121
However, when I deploy it to the Ubuntu VM, I get the following error on the class library dependency:
Project corecrossplatform (.NETCoreApp,Version=v1.0) will be compiled because the version or bitness of the CLI changed since the last build
Compiling corecrossplatform for .NETCoreApp,Version=v1.0
/opt/dotnet/corecrossplatform/project.json(24,23): error NU1002: The dependency DataAccess does not support framework .NETCoreApp,Version=v1.0.
Compilation failed.
0 Warning(s)
1 Error(s)
Time elapsed 00:00:00.0187782
This happens whether I run "dotnet restore" or "dotnet run". To be perfectly honest, I'm not even sure I'm deploying this thing correctly. Documentation is spotty and I'm making some guesses. I copied everything from the project folder "src\CoreCrossPlatform" (contains bin, Program.cs, appsettings.json, etc.) onto the VM and this is where I'm executing the "dotnet" commands from, in the VM.
The DataAccess .json file:
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.EntityFrameworkCore": "1.0.0",
"MySql.Data.Core": "7.0.4-IR-191",
"MySql.Data.EntityFrameworkCore": "7.0.4-IR-191",
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
The MVC project.json:
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"DataAccess": "1.0.0-*"
},
"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Something to note: When I run the sample project using "dotnet new" and restore, run vis the tutorial link above, it runs fine.
What am I missing? Another side question: What's the best way to publish this type of app to a Linux box? Am I even close on that part?
Thanks much.
EDIT: While kicking this dead horse all afternoon, I compared some notes I found online, related to this "NU1002" error, and the sample project "dotnet new" generates. I tried changing the "framework" section of both project.json files (MVC and classlib) to the following, with no success...same error:
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net45+win8"
]
}
}
EDIT: Much thanks to goaty: As he pointed out in the comments, copying over the entire solution and building it, results in a successful build. However, I cannot run it without an error. It doesn't seem to restore the MySQL EF Core dependency:
Project CoreCrossPlatformFlat (.NETCoreApp,Version=v1.0) will be compiled because the version or bitness of the CLI changed since the last build
Compiling CoreCrossPlatformFlat for .NETCoreApp,Version=v1.0
/opt/dotnet/corecrossplatform/src/CoreCrossPlatformFlat/project.json(25,52): error NU1001: The dependency MySql.Data.EntityFrameworkCore >= 7.0.4-IR-191 could not be resolved.
Compilation failed.
0 Warning(s)
1 Error(s)
The DataAccess library exists outside of your src/ directory. Therefore the web project could not find the reference.
I recommend this structure
src/
|---DataAccess/
|---CoreCrossPlatform/
Hope that helps :)