os.EOL in Typescript+Node.js

How to use os.EOL in Typescript Node.js program?

import { EOL } from 'os';

console.log("text"+EOL);       // ???

This is not working.

I downloaded os with npm i os -S but in node_modules/os/index.js file is only one line: module.exports = require('os'). I don't get it..


Solution 1:

Its a commonjs module and not an es6 module. So you cannot use the

import {EOL} from 'os'

style because EOL isn't exported.

You import these modules using either

import * as os from 'os';

or

import os = require('os');

the former being more common as far as I've seen.

import * as os from 'os';
const { EOL } = os;
console.log("hello" + EOL + "world");

you may or may not need to npm install @types/node for typescript to know about os. with the types installed you can explicitly tell node to load the types up with

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "types": [
      "node"
    ]
  }
}