REPL 简介
read eval print loop,交互式解释器。
Node.js 提供的一个交互式运行环境,可以做一些简单的应用程序的测试或调试
交互式解释器(REPL)既可以作为一个独立的程序运行,也可以很容易地包含在其他程序中作为整体程序的一部分使用。REPL为运行JavaScript脚本 与查看运行结果提供了一种交互方式,通常REPL交互方式可以用于调试、测试以及试验某种想法
使用
在终端输入 node
,回车即可进入 repl 环境
输入的任何表达式,enter之后,都会显示该表达式的执行结果
示例
布尔验证
使用下划线访问最近使用的表达式,但是不影响变量的值
基础命令 .help
查看所有基础命令
1 2 3 4 5 6 7 8 > .help .break Sometimes you get stuck, this gets you out .clear Alias for .break .editor Enter editor mode .exit Exit the repl .help Print this help message .load Load JS from a file into the REPL session .save Save all evaluated commands in this REPL session to a file
导入npm包 进入 repl 环境,输入module
,输出中的paths
字段显示当前 repl 加载请求包的路径。如果导入的包在列出的路径中,则可以成功加载,否则加载失败
通过环境变量NODE_PATH
导入自定义路径
1 lfp@hp ~ NODE_PATH=/THIS-IS-A/SPECIAL-PATH/I-JUST-ADDED node
定义别名
1 alias noder='NODE_PATH=/home/lfp/dev/learn-something/node_modules node'
导入方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 lfp@hp ~ $ noder Welcome to Node.js v14.18.1. Type ".help" for more information. > > module Module { id : '<repl>' , path: '.' , exports: {}, parent: undefined, filename: null, loaded: false , children: [], paths: [ '/home/lfp/repl/node_modules' , '/home/lfp/node_modules' , '/home/node_modules' , '/node_modules' , '/home/lfp/dev/learn-something/node_modules' , '/home/lfp/.node_modules' , '/home/lfp/.node_libraries' , '/home/lfp/n/lib/node' ] } > const moment = require('moment' ) undefined > moment().toDate().getTime() 1653029658323