In the Nodejs docs, I see:
import EventEmitter from 'events';
import { readFile } from 'fs';
import fs, { readFileSync } from 'fs';
.html
But with "readlines", I see:
const readline = require('readline');
.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.
In the Nodejs docs, I see:
import EventEmitter from 'events';
import { readFile } from 'fs';
import fs, { readFileSync } from 'fs';
https://nodejs/api/esm.html
But with "readlines", I see:
const readline = require('readline');
https://nodejs/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.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 27, 2018 at 19:41 Steven ChoiSteven Choi 7399 silver badges15 bronze badges 3-
2
Did you ran node with
--experimental-modules
flag? – m1ch4ls Commented Aug 27, 2018 at 19:44 -
Thanks! Doing that and renaming extension to
.mjs
worked! nodejs/api/esm.html#esm_enabling – Steven Choi Commented Aug 28, 2018 at 7:59 -
For those running across this question in the year of 2024, and beyond, we are now allowed to import
readline
on Node versions22.4
+ viaimport * as readline from 'node:readline';
– S0AndS0 Commented Jul 6, 2024 at 18:16
1 Answer
Reset to default 4The 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
.