João Alves
João Alves

Reputation: 121

NodeJS fs.writeFile fails on linux when the path has spaces

I'm using the module 'fs' to perform some OS integration and one of the procedures I do is to create/write files on the system. I've noticed that when using the function 'writeFile' and the path has spaces the procedure fails.

eg (working):

fs.writeFile('/home/john/mypath/myfile.txt', 'blabla', function() {})

eg (failing):

fs.writeFile('/home/john/my path/myfile.txt', 'blabla', function() {})

The error I'm getting is the following:

{ [Error: ENOENT: no such file or directory'] errno: -2, code: 'ENOENT', syscall: 'open', path: '"/home/john/my path/another cool/project.txt"' }

Any insights on how to workaround this? Thanks!

Upvotes: 0

Views: 3221

Answers (2)

João Alves
João Alves

Reputation: 121

After updating node the problem no longer exists..

Upvotes: 1

Christian Colón
Christian Colón

Reputation: 106

You need to escape the spaces with a backwards slash. ie ..

fs.writeFile('/home/john/my\ path/myfile.txt', 'blabla', function() {})

Upvotes: 0

Related Questions