Reputation: 163334
Is there a constant available in Node.JS for a newline character that is specific to the platform the application is running on?
For example:
\r\n
\n
Upvotes: 148
Views: 60719
Reputation: 7947
There is a constant os.EOL
:
import { EOL } from "node:os";
or
const { EOL } = require("os");
EOL
then has a value of "\n"
on POSIX or "\r\n"
on Windows.
Upvotes: 299
Reputation: 6426
The above answer is good for server-side only JavaScript with Node.js. If you have a package that serves both browser and server-side JavaScript then you want to support both:
const IS_WIN: boolean = typeof process !== 'undefined' && process.platform === 'win32';
const LINE_END: string = IS_WIN ? '\r\n' : '\n';
Upvotes: -1