Os.EOL in Typescript + Node.js

How to use os.EOL

a Typescript Node.js program?

import { EOL } from 'os';

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

      

This does not work.

I uploaded os

using npm i os -S

, but the file node_modules/os/index.js

is only one line: module.exports = require('os')

. I do not understand.

+3


source to share


3 answers


Its a generic module, not an es6 module. Therefore you cannot use

import {EOL} from 'os'

      

since EOL is not exported.

You import these modules using

import * as os from 'os';

      

or



import os = require('os');

      

the first one was more common as far as I saw.

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

      

you may need npm install @types/node

os. with types set you can explicitly tell node to load types with

tsconfig.json:

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

      

+1


source


What's wrong with the built-in node function?

const os = require('os');

console.log(os.EOL);

      

or in TypeScript style:



import os = require('os');

console.log(os.EOL);

      

using ES module syntax with import from CommonJS package is an unstable situation at the moment and should be used with caution if ever. This is especially true when using TypeScript with

--module commonjs

      

0


source


You can use the os library like this:

import * as os from "os";

os.EOL...
os.CpuInfo....

      

0


source







All Articles