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.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.