Error when trying to locate a json file, node (discord.js) - node.js

I'm getting an error when I try to find a file from my folder,
internal/modules/cjs/loader.js:969
throw err;
^
Error: Cannot find module 'badwords.json'
Here is the current code.
client.on("message", message => {
const badwords = require('badwords.json');
I can assure the file is in the same folder, people say it's a problem with my directory, I'm not too sure how to sort this out.
Thanks!

Related

node.js gm write command giving me "Error: spawn ENOTDIR"

I'm trying to create a little script to resize images. But what ever i do, i get the Error: spawn ENOTDIR
See the rwx file settings here:
I have striped the code to this. And the error still occurs:
const gm = require('gm');
gm('./test-photo.jpg').write('./output.jpg', (err) => {
console.log(err);
});
My package.json looks like this:
This is what i get:
node version:
I checked allot of examples and searched for others having the same problem. Could not find any, so i must be me forgetting something really silly.

Facing Error: Cannot find module 'C:\Users\DELL\Desktop\node js programs\file' while running Node Js server

``
This is my code for running Node js file system
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return its content:
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(3000);
``
> This is the error iam facing while running the code
internal/modules/cjs/loader.js:985 throw err; ^enter code here
Error: Cannot find module 'C:\Users\DELL\Desktop\node js programs\file'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Function.executeUserEntryPoint [as runMain] (
{ code: 'MODULE_NOT_FOUND', requireStack: [] }
For me this error occurred due to incorrect path specified. Initially, I was trying this path
"C:\Users\abc\Documents\VS CODE\JS> node basic.js"
but the actual path was
"C:\Users\abc\Documents\VS CODE\JS\demo> node basic.js"
Check if you have the correct path of your file. As when you open your vscode, the terminal is set to your workspace folder not for the folder where your files actually are.
NOTE:
Delete npm and npm-cache folder from C:\Users\appdata\roaming
Add "C:\Program Files\nodejs\node_modules\npm" to your path variable.
Do you have a folder in your project node_modules?
I suspect you don't.
Please run npm install in your project directory.
This error occurs when you have specified the wrong path. Check your directory in which you are executing
Also it happens if your folder name and the file name you are trying to execute is same.
For example:
Folder name: scrape
Js file name: scrape.js
and from /scrape directory you are trying to use node scrape.js then compiler got confused.
This error generally occurs If you require some modules or files, which do not exists in that specified path, Please check for your required modules/imports.

ENOENT error when using fs.writeFile

Trying to write to a file using fs.writeFile into a sibling directory. This works fine when using Sitemap.xml into the same directory, but not with the relative path. The public directory exists and it gives the same error whether or not Sitemap.xml exists.
Relevant dir structure:
/public
Sitemap.xml
app files
/create-sitemap
index.js - file containing code below
app.js
fs.write('../public/Sitemap.xml', data.toString(), function(err) {
if (err) throw err;
console.log("Wrote sitemap to XML");
});
Toms-MacBook-Pro:moviehunter tomchambers$ node create-sitemap/index.js
/Users/tomchambers/projects/project/create-sitemap/index.js:88
if (err) throw err;
^
Error: ENOENT, open '../public/Sitemap.xml'
When you use relative paths in node, they're related to the node process. So, if you run your script like node create-sitemap/index.js from the /Users/tomchambers/projects/project/ directory, it'll look for the /Users/tomchambers/projects/public/Sitemap.xml file, which doesn't exist.
In your case, you could use the __dirname global variable, that returns, as the docs say:
The name of the directory that the currently executing script resides in.
So your code should looks like this:
var path = require('path');
fs.write(path.join(__dirname, '../public/Sitemap.xml'), data.toString(), function(err) {
if (err) throw err;
console.log("Wrote sitemap to XML");
});
For me the problem was that the given filename contained unallowed characters on Windows.
Specifically, I tried adding a timestamp to the name e.g. 10:23:11 and the : were not allowed which caused this error.

Err. ENOENT when renaming a file in node.js

I'm trying to upload a file in my node/express app, but everytime I've got the ENOENT error when renaming the file. My code is that:
var tmp_path = req.files.file.path;
fs.rename(tmp_path, target_path, function (err) {
if(err) throw err;
...
});
where target_path will be the destination path. If I do:
console.log('exists ' + fs.existsSync(tmp_path));
then my server logs:
exists true
Also, listing the contents of tmp directory shows that the file is there. What's the problem?
FS methods like fs.rename which create, move or rename files expect that any directories in the path already exist. When they do not, you'll get an ENOENT. Since very often what you mean is "make this file -- and any directories in the path I specify for it" you may want to consider using an NPM library that abstracts access to fs with methods that take care of such things.
There are quite a few options. For example fs-extra is one of the better-tested libraries. Using fs-extra you can use ensureDir in that operation to make the directory structure first if it does not yet exist.

unable connecting to svn using the svn-spawn node module

I was actually checking on connecting to svn repository from javascript using some wrapper, but was unable to find any such js wrapper. So I found this svn-spawn node module which lets us to connect to the svn repo. But I think I am doing something wrong, not sure where and what. Need some pointers on how to go about resolving the error:
the code :
var Client = require('svn-spawn');
var client = new Client({
cwd: 'http://support.googlecode.com/svn/trunk/'
});
client.cmd(['checkout'], function(err, data) {
if(!err){
console.log('subcommand done' + data);
}else{
console.log('the erro is '+ err);
}
});
the erro that I am getting:
the erro is Error: spawn ENOENT
chdir(): No such file or directory
the erro is Error: chdir(): No such file or directory
The cwd argument is not a URL to the remote repository. Rather, it's a local directory name, when you wish your "current directory" to be, as if you invoked svn from there. Presumably the directory containing .svn.

Resources