Brad
Brad

Reputation: 163334

Node.JS constant for platform-specific new line?

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:

Upvotes: 148

Views: 60719

Answers (2)

Will Munn
Will Munn

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

Kim T
Kim T

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

Related Questions