% node Welcome to Node.js v18.18.0. Type ".help"for more information. > > > > > (To exit, press Ctrl+C again or Ctrl+D or type .exit) >
Node REPL
和浏览器中的console界面类似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
% node Welcome to Node.js v18.18.0. Type ".help"for more information. > .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
Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL > 1 + 2 3 > "hello" + "world" 'helloworld'
for(let i = 0; i < 10; i++) { console.log("hello from first script!!") }
terminal通过命令行node filename运行
1 2 3 4 5 6 7 8 9 10 11 12 13
% touch firstTest.js % node firstTest.js hello from first script!! hello from first script!! hello from first script!! hello from first script!! hello from first script!! hello from first script!! hello from first script!! hello from first script!! hello from first script!! hello from first script!! %
命令行传入参数
vs code 编辑脚本文件
通过process.argv.slice(2)获得参数的list
其中argv的第一个’/usr/local/bin/node’是运行路径, 第二个’/Users/code/args.js’是运行的脚本文件所在路径
1 2 3 4 5
console.log("hello from args", process.argv); const personList = process.argv.slice(2); for (p of personList) { console.log(`hello, ${p}`); }