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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With