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

In the Nodejs docs, I see:import EventEmitter from 'events';import { readFile } from 'f

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744695722a4588478.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信