NodeJs: fs.unlink does not delete file [VS]
I am aware this isn't the first post about fs.unlink not working, but I'm very new to both Visual Studio and Node Js.
I want to delete a file in the working folder, I got an error and the file is not deleted.
Here is what I tried:
var fs = require('fs');
fs.unlink('test1.txt');
PS: I installed the necessary Node Js components in VS.
javascript node.js visual-studio
|
show 1 more comment
I am aware this isn't the first post about fs.unlink not working, but I'm very new to both Visual Studio and Node Js.
I want to delete a file in the working folder, I got an error and the file is not deleted.
Here is what I tried:
var fs = require('fs');
fs.unlink('test1.txt');
PS: I installed the necessary Node Js components in VS.
javascript node.js visual-studio
Are you using Visual Studio, or Visual Studio Code? They are very different products. You will get different answers depending on that.
– dvsoukup
Jan 2 at 20:12
@dvsoukup Hi! thank you for responding. I created a new Javascript file inside VS and wrote the code there. Does that make it VS code?
– Haley A
Jan 2 at 20:19
It's like a modern-day Abbott and Costello sketch... Visual Studio Code is the name of an IDE application, which is different from the application that's just called Visual Studio. Does your application call itself Visual Studio Code (in the title bar, menu bar, or icon)? Or does it just call itself Visual Studio?
– IceMetalPunk
Jan 2 at 21:07
@IceMetalPunk Hi! I doubt it's Visual Studio Code. I checked and it's: Microsoft Visual Studio/Visual Studio 2017/Visual Studio Community (<I chose the last title in Microsoft's download page).
– Haley A
Jan 2 at 21:13
It doesn’t matter what ide is being used. It’s Node....
– Heretic Monkey
Jan 2 at 21:14
|
show 1 more comment
I am aware this isn't the first post about fs.unlink not working, but I'm very new to both Visual Studio and Node Js.
I want to delete a file in the working folder, I got an error and the file is not deleted.
Here is what I tried:
var fs = require('fs');
fs.unlink('test1.txt');
PS: I installed the necessary Node Js components in VS.
javascript node.js visual-studio
I am aware this isn't the first post about fs.unlink not working, but I'm very new to both Visual Studio and Node Js.
I want to delete a file in the working folder, I got an error and the file is not deleted.
Here is what I tried:
var fs = require('fs');
fs.unlink('test1.txt');
PS: I installed the necessary Node Js components in VS.
javascript node.js visual-studio
javascript node.js visual-studio
edited Jan 2 at 20:54
Haley A
asked Jan 2 at 19:59
Haley AHaley A
134
134
Are you using Visual Studio, or Visual Studio Code? They are very different products. You will get different answers depending on that.
– dvsoukup
Jan 2 at 20:12
@dvsoukup Hi! thank you for responding. I created a new Javascript file inside VS and wrote the code there. Does that make it VS code?
– Haley A
Jan 2 at 20:19
It's like a modern-day Abbott and Costello sketch... Visual Studio Code is the name of an IDE application, which is different from the application that's just called Visual Studio. Does your application call itself Visual Studio Code (in the title bar, menu bar, or icon)? Or does it just call itself Visual Studio?
– IceMetalPunk
Jan 2 at 21:07
@IceMetalPunk Hi! I doubt it's Visual Studio Code. I checked and it's: Microsoft Visual Studio/Visual Studio 2017/Visual Studio Community (<I chose the last title in Microsoft's download page).
– Haley A
Jan 2 at 21:13
It doesn’t matter what ide is being used. It’s Node....
– Heretic Monkey
Jan 2 at 21:14
|
show 1 more comment
Are you using Visual Studio, or Visual Studio Code? They are very different products. You will get different answers depending on that.
– dvsoukup
Jan 2 at 20:12
@dvsoukup Hi! thank you for responding. I created a new Javascript file inside VS and wrote the code there. Does that make it VS code?
– Haley A
Jan 2 at 20:19
It's like a modern-day Abbott and Costello sketch... Visual Studio Code is the name of an IDE application, which is different from the application that's just called Visual Studio. Does your application call itself Visual Studio Code (in the title bar, menu bar, or icon)? Or does it just call itself Visual Studio?
– IceMetalPunk
Jan 2 at 21:07
@IceMetalPunk Hi! I doubt it's Visual Studio Code. I checked and it's: Microsoft Visual Studio/Visual Studio 2017/Visual Studio Community (<I chose the last title in Microsoft's download page).
– Haley A
Jan 2 at 21:13
It doesn’t matter what ide is being used. It’s Node....
– Heretic Monkey
Jan 2 at 21:14
Are you using Visual Studio, or Visual Studio Code? They are very different products. You will get different answers depending on that.
– dvsoukup
Jan 2 at 20:12
Are you using Visual Studio, or Visual Studio Code? They are very different products. You will get different answers depending on that.
– dvsoukup
Jan 2 at 20:12
@dvsoukup Hi! thank you for responding. I created a new Javascript file inside VS and wrote the code there. Does that make it VS code?
– Haley A
Jan 2 at 20:19
@dvsoukup Hi! thank you for responding. I created a new Javascript file inside VS and wrote the code there. Does that make it VS code?
– Haley A
Jan 2 at 20:19
It's like a modern-day Abbott and Costello sketch... Visual Studio Code is the name of an IDE application, which is different from the application that's just called Visual Studio. Does your application call itself Visual Studio Code (in the title bar, menu bar, or icon)? Or does it just call itself Visual Studio?
– IceMetalPunk
Jan 2 at 21:07
It's like a modern-day Abbott and Costello sketch... Visual Studio Code is the name of an IDE application, which is different from the application that's just called Visual Studio. Does your application call itself Visual Studio Code (in the title bar, menu bar, or icon)? Or does it just call itself Visual Studio?
– IceMetalPunk
Jan 2 at 21:07
@IceMetalPunk Hi! I doubt it's Visual Studio Code. I checked and it's: Microsoft Visual Studio/Visual Studio 2017/Visual Studio Community (<I chose the last title in Microsoft's download page).
– Haley A
Jan 2 at 21:13
@IceMetalPunk Hi! I doubt it's Visual Studio Code. I checked and it's: Microsoft Visual Studio/Visual Studio 2017/Visual Studio Community (<I chose the last title in Microsoft's download page).
– Haley A
Jan 2 at 21:13
It doesn’t matter what ide is being used. It’s Node....
– Heretic Monkey
Jan 2 at 21:14
It doesn’t matter what ide is being used. It’s Node....
– Heretic Monkey
Jan 2 at 21:14
|
show 1 more comment
1 Answer
1
active
oldest
votes
As far as the code goes, you're not invoking fs.unlink
properly. For starters, it's asynchronous. You will need to provide it a callback. See example here:
https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback
Secondly, you need to provide it the full file path, not just the name of the file... ie:
var fs = require('fs');
fs.unlink('C:pathtomyfiletest1.txt', (err) => {});
You can also supply it with the variable __dirname
to utilize your current working directory from wherever you invoke node against the script. Thus, that would look something like:
let fs = require('fs');
let path = require('path');
fs.unlink(path.join(__dirname, 'test1.txt', (err) => {
if (err) throw err;
console.log('test1.txt was deleted');
});
Currently, you can also invoke it synchronously using it's single parameter signature... thus you'd provide only the dir path:
fs.unlinkSync('C:\path\to\my\file\test1.txt');
But, this is ill-advised as it will be blocking. I'd only use the "sync" variant during some application bootstrapping process, where it'd be invoked only one time or so, at startup. Try to fight the urge of it being "easier" to use and understand, and instead get yourself to understand asynchronous logic.
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012417%2fnodejs-fs-unlink-does-not-delete-file-vs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As far as the code goes, you're not invoking fs.unlink
properly. For starters, it's asynchronous. You will need to provide it a callback. See example here:
https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback
Secondly, you need to provide it the full file path, not just the name of the file... ie:
var fs = require('fs');
fs.unlink('C:pathtomyfiletest1.txt', (err) => {});
You can also supply it with the variable __dirname
to utilize your current working directory from wherever you invoke node against the script. Thus, that would look something like:
let fs = require('fs');
let path = require('path');
fs.unlink(path.join(__dirname, 'test1.txt', (err) => {
if (err) throw err;
console.log('test1.txt was deleted');
});
Currently, you can also invoke it synchronously using it's single parameter signature... thus you'd provide only the dir path:
fs.unlinkSync('C:\path\to\my\file\test1.txt');
But, this is ill-advised as it will be blocking. I'd only use the "sync" variant during some application bootstrapping process, where it'd be invoked only one time or so, at startup. Try to fight the urge of it being "easier" to use and understand, and instead get yourself to understand asynchronous logic.
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
|
show 3 more comments
As far as the code goes, you're not invoking fs.unlink
properly. For starters, it's asynchronous. You will need to provide it a callback. See example here:
https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback
Secondly, you need to provide it the full file path, not just the name of the file... ie:
var fs = require('fs');
fs.unlink('C:pathtomyfiletest1.txt', (err) => {});
You can also supply it with the variable __dirname
to utilize your current working directory from wherever you invoke node against the script. Thus, that would look something like:
let fs = require('fs');
let path = require('path');
fs.unlink(path.join(__dirname, 'test1.txt', (err) => {
if (err) throw err;
console.log('test1.txt was deleted');
});
Currently, you can also invoke it synchronously using it's single parameter signature... thus you'd provide only the dir path:
fs.unlinkSync('C:\path\to\my\file\test1.txt');
But, this is ill-advised as it will be blocking. I'd only use the "sync" variant during some application bootstrapping process, where it'd be invoked only one time or so, at startup. Try to fight the urge of it being "easier" to use and understand, and instead get yourself to understand asynchronous logic.
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
|
show 3 more comments
As far as the code goes, you're not invoking fs.unlink
properly. For starters, it's asynchronous. You will need to provide it a callback. See example here:
https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback
Secondly, you need to provide it the full file path, not just the name of the file... ie:
var fs = require('fs');
fs.unlink('C:pathtomyfiletest1.txt', (err) => {});
You can also supply it with the variable __dirname
to utilize your current working directory from wherever you invoke node against the script. Thus, that would look something like:
let fs = require('fs');
let path = require('path');
fs.unlink(path.join(__dirname, 'test1.txt', (err) => {
if (err) throw err;
console.log('test1.txt was deleted');
});
Currently, you can also invoke it synchronously using it's single parameter signature... thus you'd provide only the dir path:
fs.unlinkSync('C:\path\to\my\file\test1.txt');
But, this is ill-advised as it will be blocking. I'd only use the "sync" variant during some application bootstrapping process, where it'd be invoked only one time or so, at startup. Try to fight the urge of it being "easier" to use and understand, and instead get yourself to understand asynchronous logic.
As far as the code goes, you're not invoking fs.unlink
properly. For starters, it's asynchronous. You will need to provide it a callback. See example here:
https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback
Secondly, you need to provide it the full file path, not just the name of the file... ie:
var fs = require('fs');
fs.unlink('C:pathtomyfiletest1.txt', (err) => {});
You can also supply it with the variable __dirname
to utilize your current working directory from wherever you invoke node against the script. Thus, that would look something like:
let fs = require('fs');
let path = require('path');
fs.unlink(path.join(__dirname, 'test1.txt', (err) => {
if (err) throw err;
console.log('test1.txt was deleted');
});
Currently, you can also invoke it synchronously using it's single parameter signature... thus you'd provide only the dir path:
fs.unlinkSync('C:\path\to\my\file\test1.txt');
But, this is ill-advised as it will be blocking. I'd only use the "sync" variant during some application bootstrapping process, where it'd be invoked only one time or so, at startup. Try to fight the urge of it being "easier" to use and understand, and instead get yourself to understand asynchronous logic.
edited Jan 2 at 21:26
answered Jan 2 at 20:18
dvsoukupdvsoukup
1,062923
1,062923
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
|
show 3 more comments
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Thanks @dvsoukup for the link. So the callback would be handling the error. I did modify the code following this method, and I got this error: [Error: ENOENT: no such file or directory, unlink 'pathtest1.txt']
– Haley A
Jan 2 at 21:03
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Does that path exist on your computer? I couldn't tell you. That error is thrown because the file/directory doesn't exist (hence the error description...). Pass in the full path to the file.
– dvsoukup
Jan 2 at 21:11
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
Yes, I pasted it from the folder and checked spaces and file name. Strange!
– Haley A
Jan 2 at 21:19
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
the '' is an escape character. So you probably need to use double slashes... like so: 'path\to\filetheFile.txt'. It's kind of a hassle as you can see, which is why I'd suggest you use that option to supply the __dirname variable, and concat it with path.join.
– dvsoukup
Jan 2 at 21:25
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
Is this how you define a variable: let __dirname = require('C:\Users\name\Documents\node intro\test1.txt');
– Haley A
Jan 2 at 21:40
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012417%2fnodejs-fs-unlink-does-not-delete-file-vs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you using Visual Studio, or Visual Studio Code? They are very different products. You will get different answers depending on that.
– dvsoukup
Jan 2 at 20:12
@dvsoukup Hi! thank you for responding. I created a new Javascript file inside VS and wrote the code there. Does that make it VS code?
– Haley A
Jan 2 at 20:19
It's like a modern-day Abbott and Costello sketch... Visual Studio Code is the name of an IDE application, which is different from the application that's just called Visual Studio. Does your application call itself Visual Studio Code (in the title bar, menu bar, or icon)? Or does it just call itself Visual Studio?
– IceMetalPunk
Jan 2 at 21:07
@IceMetalPunk Hi! I doubt it's Visual Studio Code. I checked and it's: Microsoft Visual Studio/Visual Studio 2017/Visual Studio Community (<I chose the last title in Microsoft's download page).
– Haley A
Jan 2 at 21:13
It doesn’t matter what ide is being used. It’s Node....
– Heretic Monkey
Jan 2 at 21:14