Atul Agrawal
Atul Agrawal

Reputation: 1520

Write a file to a directory which is different from working directory

I have my project structure something like this

enter image description here

Now, I need to write a file from assets.js to a file in pdf folder.

That is what i am trying

var qrImgPath =   '/lib/pdf/' +eod+'.png';
                    fs.writeFile(qrImgPath,body,'binary',function(err){
                        return next();
                    });

but i am getting following error

{ handle: 2,
  type: 'error',
  className: 'Error',
  constructorFunction: { ref: 5 },
  protoObject: { ref: 6 },
  prototypeObject: { ref: 1 },
  properties: 
   [ { name: 'stack',
       attributes: 2,
       propertyType: 3,
       ref: 1 },
     { name: 'arguments',
       attributes: 2,
       propertyType: 1,
       ref: 1 },
     { name: 'type',
       attributes: 2,
       propertyType: 1,
       ref: 1 },
     { name: 'message',
       attributes: 2,
       propertyType: 1,
       ref: 7 },
     { name: 'errno',
       propertyType: 1,
       ref: 8 },
     { name: 'code',
       propertyType: 1,
       ref: 9 },
     { name: 'path',
       propertyType: 1,
       ref: 10 } ],
  text: 'Error: ENOENT, open \'/lib/pdf/b0551796a741aa885e641dbd895a233f.png\'' }

.

How can i achieve this?

Upvotes: 0

Views: 40

Answers (2)

Jorge
Jorge

Reputation: 1176

You could use the __dirname global variable, that returns the name of the directory that the currently executing script resides in.

So, your code should be something like:

var qrImgPath =   path.join(__dirname, '../lib/pdf/' +eod+'.png');
                    fs.writeFile(qrImgPath,body, 'binary',function(err){
                        return next();
                    });

Upvotes: 1

krakig
krakig

Reputation: 1555

You can use __dirname. (https://nodejs.org/docs/latest/api/globals.html#globals_dirname)

path.join(__dirname, "../../lib/pdf" + eod + "png");

Upvotes: 1

Related Questions