Skip to content
nodejsbeginner

Node.js Basics Quiz

Test your understanding of Node.js runtime, modules, and async I/O.

8 questions

By EZ4Code Team

1. Node.js 默认使用的模块系统是什么?

CommonJS
ES Modules
AMD
UMD
Explanation: Node.js 默认使用 CommonJS (CJS) 模块系统,通过 require/module.exports 导入导出。ESM 需要在 package.json 设置 "type": "module" 或使用 .mjs 扩展名。

2. require() 返回的是什么?

module.exports 对象
字符串
函数
数组
Explanation: require() 返回目标模块通过 module.exports 导出的对象,可以是对象、函数、原始值等,取决于模块如何导出。

3. process.platform 返回什么?

操作系统平台
Node 版本
进程 PID
当前目录
Explanation: process.platform 返回表示操作系统平台的字符串,如 'win32'、'linux'、'darwin'。Node 版本用 process.version,PID 用 process.pid,当前目录用 process.cwd()。

4. 下列哪个是 Node.js 的内置模块?

fs
express
lodash
axios
Explanation: fs (文件系统) 是 Node.js 的内置模块,无需安装即可 require。express、lodash、axios 都是第三方 npm 包。

5. __dirname 表示什么?

当前模块所在目录
当前工作目录
可执行文件路径
用户家目录
Explanation: __dirname 是当前模块文件所在目录的绝对路径。当前工作目录用 process.cwd(),它取决于启动 Node 进程的目录,与模块位置无关。

6. Node.js 事件循环的主要阶段顺序(简化)是?

timers → poll → check
timers → check → poll
poll → timers → check
check → poll → timers
Explanation: 事件循环简化顺序为 timers(执行 setTimeout/setInterval 回调)→ poll(获取 I/O 事件)→ check(执行 setImmediate 回调)。完整循环还包含 pending callbacks、idle/prepare、close callbacks 等阶段。

7. Buffer 是什么?

二进制数据容器
字符串缓存
文件流
网络缓冲
Explanation: Buffer 是 Node.js 中用于处理二进制数据的类,类似 Uint8Array,专门用于在流中读写原始字节,如文件 I/O、网络传输。

8. npm install --save-dev 安装的包会写入哪个字段?

devDependencies
dependencies
peerDependencies
optionalDependencies
Explanation: --save-dev (-D) 将依赖写入 devDependencies,仅用于开发环境(如测试、构建工具)。dependencies 是运行时依赖,peerDependencies 是对等依赖,optionalDependencies 是可选依赖。