How to set or change line endings of a Text File in Node.JS

Sometimes text files end up with incompatible line ending types, especially when working with multiple operating systems. At times like there, it is important to have a quick fix in place that will be able to change these line endings to the desired type. Enter editTextChangeLineEndings, an API function from Cloudmersive that will allow you to do just that with a simple API call. Here’s how it works.

Begin with this command for installing our Document and Conversion API client.

npm install cloudmersive-convert-api-client --save

Now we just need to call editTextChangeLineEndings using an API instance, then specify which type of line ending we would like, choosing between Windows, Unix, and Mac.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.EditTextApi();var lineEndingType = "lineEndingType_example"; // String | Required; 'Windows' will use carriage return and line feed, 'Unix' will use newline, and 'Mac' will use carriage returnvar inputFile = "/path/to/file"; // File | Input file to perform the operation on.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editTextChangeLineEndings(lineEndingType, inputFile, callback);

There you go, super easy. Without much else to say about line endings, let me also mention that this library also has many other functions, ranging all the way from updating legacy file formats to converting document files to different formats, such as DOCX to PDF.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store