4

In the Nodejs docs, I see:

import EventEmitter from 'events';

import { readFile } from 'fs';

import fs, { readFileSync } from 'fs';

https://nodejs.org/api/esm.html

But with "readlines", I see:

const readline = require('readline');

https://nodejs.org/dist/latest-v10.x/docs/api/readline.html

But in StackOverflow, I see:

import * as readline from "readline";

TypeScript + NodeJS readline property missing

But I tried the above and other variations of import and can't make it work, so I have to use require. Could someone explain to me why this is the case, since readline is a default node module?

Thanks.

Community
  • 1
  • 1
Steven Choi
  • 669
  • 6
  • 15

1 Answers1

4

The use of import syntax is not yet available by default in Node.js LTS. You can either use a transpiler such Babel to be able to use it or use the --experimental-modules flag when running your Node.js script / server besides changing the extension of your files from .js to .mjs.

Dez
  • 5,702
  • 8
  • 42
  • 51