I want to create a CLI for bootstrap my project. Everything works find but in order to copy my template files I use ncp where I give the template directory as a argument. So I tried,
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
const access = promisify(fs.access);
...
const templateDir = path.resolve(
new URL(import.meta.url).pathname,
'../../templates',
'project_name'
);
try {
await access(templateDir, fs.constants.R_OK);
} catch (err) {
console.error('%s Invalid template name', chalk.red.bold('ERROR'));
process.exit(1);
}
...
I got ERROR Invalid template name
Then I tried to figure out the problem,
//* after getting ERROR Invalid template name
//* checking file exist or not
fs.stat(templateDir, function (err, stat) {
if (err == null) {
console.log('File exists');
} else if (err.code === 'ENOENT') {
// file does not exist
console.log("file doesn't exit");
} else {
console.log('Some other error: ', err.code);
}
});
file doesn't exit
[Error: ENOENT: no such file or directory, access 'G:\G:\CLI%20project\create-project\templates\javascript'] {
errno: -4058,
code: 'ENOENT',
syscall: 'access',
path: 'G:\\G:\\CLI%20project\\create-project\\templates\\javascript'
}
ERROR Invalid template name
I expect the access should be G:\CLI%20project\create-project\templates\javascript but it's not giving the that. where was the wrong happened? by the way I use esm module loader.
Give your errors a good read.
[Error: ENOENT: no such file or directory, access 'G:\G:\CLI%20project\create-project\templates\javascript'] {
errno: -4058,
code: 'ENOENT',
syscall: 'access',
path: 'G:\\G:\\CLI%20project\\create-project\\templates\\javascript'
}
They're not trying to trick you. They're just telling you/
G:\\G:\\
Error messages are there for a reason .
I had the same issue and found the answer:
const templateDir = path.resolve(
new URL(import.meta.url).pathname, // This is where it gives the error
'../../templates',
options.template
);
Change new URL (import.meta.url).pathname to __filename. It worked for me.
Related
so I was making a program that scrapes a website and stores the data in json format with fsPromises.writeFile and another program that reads the json file with fsPromises.readFile. the json file is written correctly as I've checked it manually but when I try to read it with fsPromises it returns the error mentioned below.
I've checked the file path and it's all correct, and I can literally see the json file in the directory and when I use require to parse the JSON file it works, but I want to use fs to check is the file is present and to parse from the file
I wrote the JSON file with fsPromises like this
await fsPromises.writeFile(
path.join(__dirname, "json", "productVarables.json"),
JSON.stringify(variables),
(err) => {
if (err) {
console.log("error while writing the JSON" + err);
} else {
console.log("successfuly written the JSON file");
}
}
);
and when I try to read it using fsPromises like this
await fsPromises.readFile(
path.join(__dirname, "json", "productVarables.json"),
"utf-8",
(err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
}
);
I get the following error
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
[Error: ENOENT: no such file or directory, open 'D:\desktop\Project\Node\Scraper\json\productVariables.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'D:\\desktop\\Project\\Node\\Scraper\\json\\productVariables.json'
}
As you can see, the directory exists and the file too, but for some reason, it's not being able to find the file to delete it.
Directory exists
Code:
const fs = require('fs')
const path = "./resources/castigos/silencios/705143456384811039/526122991042428959.json"
fs.unlink(path, (err) => {
if (err) {
console.error(err)
return
}
//file removed
})
I tried running the project as root, nothing...
[Error: ENOENT: no such file or directory, unlink './resources/castigos/silencios/705143456384811039/526122991042428959.json'] {
errno: -2,
code: 'ENOENT',
syscall: 'unlink',
path: './resources/castigos/silencios/705143456384811039/526122991042428959.json'
}
In your image file name is different, I believe wrong screen shared,
Also whenever dealing with fs and file paths use path module and __dirname
you can update your path as below
const path = require('path');
const filePath = path.join(__dirname, '/resources/castigos/silencios/705143456384811039/526122991042428959.json');
I have static folder, this:
App.use('/getUnity', Express.static(__dirname + '/uax_vr_uploads/unityFiles'));
I want delete file in folder, not working this 2 fs.unlink:
fs.unlink('http://localhost:3002/getUnity/' + response[0][item].varyant_object, (error) => {
if (error) {
console.log(error);
} else {
console.log('Deleted');
}
});
OR
fs.unlink('http://localhost:3002/uax_vr_uploads/unityFiles/' + response[0][item].varyant_object, (error) => {
if (error) {
console.log(error);
} else {
console.log('Deleted');
}
});
Console LOG:
[Error: ENOENT: no such file or directory, unlink 'http://localhost:3002/getUnity/file-1590880543263.unity3d'] {
errno: -2,
code: 'ENOENT',
syscall: 'unlink',
path: 'http://localhost:3002/getUnity/file-1590880543263.unity3d'
}
If you're trying to remove a file from your static folder, then you need to pass a valid OS path name to fs.unlink() not a URL that only your server would know about.
If the file on your disk that you're actually trying to remove is this:
/uax_vr_uploads/unityFiles/file-1590880543263.unity3d
Then, judging by your code, it looks like you want this:
fs.unlink('/uax_vr_uploads/unityFiles/' + response[0][item].varyant_object);
Having an issue with fs.writeFile in my nodejs app running locally where im getting an error of this below,
I am running on localhost xampp also on windows if that may be an issue?
[Error: ENOENT: no such file or directory, open 'C:\Users\exampleuser\Desktop\examplenodejspath\product\sku123.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\exampleuser\\Desktop\\examplenodejspath\\product\\sku123.json'
}
Below is a copy of the code.
var product = {"SKU": "sku123","name": "test"};
fs.writeFile(__dirname + "/product/" + product.SKU + ".json", product, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("product was saved!");
});
I can confirm the path is correct. but for some reason it still returns its not correct.
Any help would be appericated
I'm trying to make a download link on my server for a zip file and I'm currently getting this error: (Note, still just testing it on local machine)
{ [Error: ENOENT: no such file or directory, stat 'C:\Users\Jordan\Desktop\Websites\HappyCamel\Users\Jordan\Desktop\Websites\HappyCamel']
errno: -4058,
code: 'ENOENT',
syscall: 'stat',
path: 'C:\\Users\\Jordan\\Desktop\\Websites\\HappyCamel\\Users\\Jordan\\Desktop\\Websites\\HappyCamel',
expose: false,
statusCode: 404,
status: 404 }
The relevant code is this:
router.get('/file/:name', function(req, res, next) {
console.log('test123'); //successfully prints to console
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
console.log('test456'); //successfully prints to console
if(err) {
console.log(err) //I'm assuming this is source of logged error
} else {
console.log("no error"); //doesn't print
}
});
})
edit:
Fixed it with changing this line:
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
to
res.download('./test123.zip', 'test123.zip', function(err) {
but now I get
angular.min.js:114 ReferenceError: success is not defined
error on my browser, but no errors in my node console (my "no error" line is printing)
you are using relative path. when you do this:
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
it will look for Users/Jordan/Desktop/Websites/HappyCamel/ inside your current file's directory. looks like what you need is full path, or better a correct relative path- from the error it looks like the file is located with your code, so this should do:
res.download('./', 'test123.zip', function(err) {