5

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..

tBlabs
  • 2,619
  • 3
  • 18
  • 22

3 Answers3

4

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"
    ]
  }
}
Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
  • It is worth noting that if your environment supports it, `import os from 'os';` is preferable to `import * as os from 'os';` as the latter is TypeScript specific, non-standard node-interop that will break in the future. – Aluan Haddad May 04 '17 at 06:47
  • `import os from 'os';` is for importing es6 default exports... as of node 7.4 (or at least the latest node typings) that isn't valid. – Meirion Hughes May 04 '17 at 07:21
  • It depends on the context. If you are using Webpack, SystemJS, or piping through Babel, use the default interop require syntax. `import * as x from ...` has nothing to do with NodeJS. It is a problematic TS construct for mapping ES Module Syntax onto CommonJS. That is why I recommend `import os = require('os')` in my answer. – Aluan Haddad May 04 '17 at 07:24
  • `import os from 'os';`- TSError: ⨯ Unable to compile TypeScript example.ts (1,8): Module '"os"' has no default export. (1192) – Meirion Hughes May 04 '17 at 07:27
  • If you are targeting Node and using `--module commonjs` then use `require` if you are using another transpiler or a loader that provides interop, use `import os from 'os';` as that is what NodeJS will one day support. `import * as os` is bad legacy. That error is expected since you are running directly against node via `tsc --module commonjs` because typescrfipt does not have proper interop behavior. Please see https://github.com/nodejs/node-eps/blob/master/002-es6-modules.md – Aluan Haddad May 04 '17 at 07:28
  • To understand why `import * as ... from` is broken, consider that ES Modules prohibit a namespace from being callable or constructable. Many modules imported this way actually export a callable or constructable. – Aluan Haddad May 04 '17 at 07:41
  • `import * as os from 'os';` produces `error TS2307: Cannot find module 'os'.` And I don't wanna require() next to my imports.. That's looks bad. – tBlabs May 04 '17 at 08:29
  • @tBlabs install `npm install @types/node` and maybe add `types: ["node"]` in your `compileOptions` in tsconfig.json... that will tell typescript compiler to use node typings, which includes the `os` module. – Meirion Hughes May 04 '17 at 12:26
0

You can use os library in the following way:

import * as os from "os";

os.EOL...
os.CpuInfo....
Leonardo Venoso
  • 1,203
  • 12
  • 15
0

I know this is an old post, but now-a-days you can do this:

  1. Add compilerOptions.module & compilerOptions.esModuleInterop to tsconfig.json:
{
    "compilerOptions": {
      "module": "commonjs",
      "esModuleInterop": true
    }
}

  1. Import like a module that has a default export:
import os from 'os';
console.log(os.EOL);