Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I "import * as readline from 'readline'" in Nodejs v10?

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.

like image 594
Steven Choi Avatar asked Oct 20 '25 09:10

Steven Choi


1 Answers

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.

like image 76
Dez Avatar answered Oct 21 '25 22:10

Dez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!