0

I am facing one issue. I need to append \n inside file using node js fs module. I am explaining my code below.

let endStr = '   ret= sh(script:"touch robot-rfs.yml && rm -rf robot-rfs.yml  ")\n'
           + 'nedarray = list_of_neds.split("\n")';

fs.writeFileSync(`${process.env['root_dir']}/uploads/Jenkinsfile`, endStr);

Here my expected output should be like below after file writing.

Jenkinsfile:

ret= sh(script:"touch robot-rfs.yml && rm -rf robot-rfs.yml  ")
nedarray = list_of_neds.split("\n")

But I am getting the output like below.

ret= sh(script:"touch robot-rfs.yml && rm -rf robot-rfs.yml  ")
   nedarray = list_of_neds.split("
   ")

Here due to \n inside split its taking to new line. I need to write with \n without any line break inside the file means nedarray = list_of_neds.split("\n") should be written directly in the file. Can anyone give the help to resolve this issue ?

subhra_user
  • 439
  • 5
  • 19
  • 2
    backslashes inside a string literal need to be escaped if you want them to be literal backslashes. So `'\\n'` will be a character string with 2 characters. – trincot Dec 24 '20 at 09:19
  • @trincot - Did you mean a *two* character string? The backslash, and the `n`? – T.J. Crowder Dec 24 '20 at 09:21

0 Answers0