It is not possible to display line breaking strings in Node.js

Asked

Viewed 338 times

0

I need to work with css in a js script for nodejs. However, if css has any line breaks the console.log does not work.

const fs = require('fs');
var contents = fs.readFileSync('style.css').toString();
console.log(contents);

However its I take the spaces of to see that it was loaded the content:

console.log(contents.replace(/\s/g, "\r"););

The problem is that I need to display with all the line breaks in my project.

I am inciting at Nodejs. If anyone can explain to me why this is and how to solve.

I’m running nodejs inside php exec:

$exec = exec($nodejs." p.js ", $output);

echo $exec;
  • Show the css file there

  • https://jsfiddle.net/6uLjr37o/

  • Your code works correctly on my machine see in the image https://imagebin.ca/v/4932HMtdCHdt

  • I’m using inside an exec in php on Windows system, this will be the problem?

  • I just tried debian 9 linux system on my server and it doesn’t work either.

  • I’ll try it on Linux

Show 1 more comment

1 answer

0

After many tests I discovered that every line break in php is received by $output as an array.

echo at $exec only picks up the last line break.

This way to display the content was just using a foreach!

$exec = exec($nodejs." p.js ", $output);

foreach($output as $line){ echo $line."\n";}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.