맥에서 nvm 을 설치하고 nodejs를 실행하는 방법입니다. NVM을 이용하여 여러버전의 nodejs를 사용할 수 있습니다.
NVM 설치
Install NVM (version manager) via Homebrew (Recommended)
create a profile for zsh
touch .zshrc
get download a script to install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh
| bash
Apply bash
source ~/.zshrc
check NVM version
nvm --version
nvm ls-remote
install LTS version
nvm install v20.11.9
node --version
nvm ls
nvm install v18.19.0
nvm use <version>
https://nodejs.org/docs/latest/api/synopsis.html#example
Usage and example | Node.js v23.9.0 Documentation
Usage and example# Usage# node [options] [V8 options] [script.js | -e "script" | - ] [arguments] Please see the Command-line options document for more information. Example# An example of a web server written with Node.js which responds with 'Hello, World!'
nodejs.org
위의 링크를 참조하여 샘플 js 을 실행하는 방법입니다.
mkdir node-test
cd node-test
Visual Studio Code에서 hello-world.js 파일을 생성하고 다음의 코드를 입력합니다.
const http = require('node:http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
터미널에서 다음을 실행하여 웹페이지가 로딩 되는 것을 확인합니다.
node hello-world.js
Server running at http://127.0.0.1:3000/
vscode를 code . 명령으로 터미널에서 실행하기
터미널에서 code .
으로 열려고 할 때 zsh: command not found: code
에러가 발행하면 다음의 방법으로 해결한다.
vscode를 실행한 후에 cmd + shift + P로 명령어 팔렛트를 열고 다음의 명령을 입력한다.
Shell Command: Install 'code' command in PATH
'Programming > Node JS' 카테고리의 다른 글
Node Js 디버깅 (0) | 2017.03.07 |
---|