最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 versions 22.4+ via import * as readline from 'node:readline'; – S0AndS0 Commented Jul 6, 2024 at 18:16
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论