0

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?

Atul Agrawal
  • 1,474
  • 5
  • 22
  • 41
  • Use a relative path, like so: `var qrImgPath = '../../lib/pdf/' +eod+'.png';` – jonny Apr 20 '16 at 08:45
  • @JonathanBrooks still same error – Atul Agrawal Apr 20 '16 at 08:49
  • This error means `/lib/pdf/b0551796a741aa885e641dbd895a233f.png` does not exist. You can run `fs.existsSync('/lib/pdf/b0551796a741aa885e641dbd895a233f.png')` to test for its existance. – Lewis Apr 20 '16 at 09:05
  • if i am not wrong fs.writeFile creates the file if does not exists. my problem is fs is not able to switch directory from "routes" to "lib" where i wants to write the file – Atul Agrawal Apr 20 '16 at 09:07
  • @AtulAgrawal Correct. But it can't create the file with missing parent folders. Make sure that the path is correct. – Lewis Apr 20 '16 at 09:10
  • parent folder is there as you can see in my screen shot of project structure.As i said in last comment i am not able to switch from one directory to other – Atul Agrawal Apr 20 '16 at 09:12
  • Your path is incorrect because the `root` path is missing. The correct one should be something similar to `ROOT_DIR + '/lib/pdf/b0551796a741aa885e641dbd895a233f.png'`. See [this question](http://stackoverflow.com/questions/10265798/determine-project-root-from-a-running-node-js-application) for more information about defining root directory. – Lewis Apr 20 '16 at 09:18
  • Please see krakig's answer – Atul Agrawal Apr 20 '16 at 09:20

2 Answers2

1

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

path.join(__dirname, "../../lib/pdf" + eod + "png");
krakig
  • 1,515
  • 1
  • 19
  • 33
1

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();
                    });
Jorge
  • 1,136
  • 9
  • 15
  • Unfortunately no success – Atul Agrawal Apr 20 '16 at 08:53
  • this is what i am getting Error: ENOENT, open \'/home/atul/Workplace/market-invoice/routes/lib/pdf/b0551796a741aa885e641dbd895a233f.png\' – Atul Agrawal Apr 20 '16 at 08:54
  • Change this line: var qrImgPath = '../../lib/pdf/' +eod+'.png'; – Jorge Apr 20 '16 at 08:56
  • Error: ENOENT, open \'/home/atul/Workplace/market-invoice/routes../../lib/pdf/b0551796a741aa885e641dbd895a233f.png\' – Atul Agrawal Apr 20 '16 at 09:02
  • Man __dirname returns the current directory which is '/home/atul/Workplace/market-invoice/routes and when i add my path it does not change the directory. i need something like this '/home/atul/Workplace/market-invoice/lib/pdf ' which is the actual path where i need to write my file – Atul Agrawal Apr 20 '16 at 09:04
  • You're right... see my edit, using path.join should work fine – Jorge Apr 20 '16 at 09:21