WebSocket이로 채팅 기능을 구현해 보았다.
현재 파일구조
websocket-chatting
├─ README.md
├─ back
│ ├─ package-lock.json
│ ├─ package.json
│ └─ src
│ └─ server.js
└─ front
package.json 파일 생성
npm init -y
node modules & package-lock.json 파일 생성
npm install express
nodemon
nodemon 이란?
Node.js 서버가 실행 중일 때 코드에 변경사항이 생기면 서버를 내리고 다시 가동을 시켜야 한다. 이때, nodemon을 이용하면 굳이 서버를 내리지 않아도 변화된 부분을 반영시켜 주는 Node.js 패키지이다.
설치
npm install nodemon --save-dev
package.json 파일에 nodemon이 설치된 것이 보인다.
{
"name": "back",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
Error #1 : Cannot use import statement outside a module
(node:93716) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/heera/Desktop/Web/git/websocket-chatting/back/src/index.js:1
import express from 'express';
^^^^^^
SyntaxError: Cannot use import statement outside a module
해결
"type": "module" 추가해 줌으로서 에러가 해결되었다.
{
"name": "back",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
}
...
}
Error #2 : 파일 위치를 찾지 못함
실행명령어 입력 후 아래와 같은 에러가 떴다.
nodemon src/server
에러 메시지
command not found: nodemon
해결
아래 명령어로 설치했지만
npm install -g nodemon
아래와 같은 애러가 뜨면서 설치가 되지 않았다.
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
맥에서는 종종 뜨는 문구라 아래와 같이 sudo를 붙여 다시 설치해주었다.
(password 뜨면 mac로그인 비번 입력하면 된다.)
sudo npm install -g nodemon
실행 명령어
npm run start
3030 포트에 서버 뛰우기 성공!
'개발 일지 > TIL' 카테고리의 다른 글
[ React Query ] 게시물에 따른 comments 업데이트 (0) | 2023.01.21 |
---|---|
[ React Query ] useQuery, isError, isLoading (0) | 2023.01.19 |
[ TypeScript ] 제네릭 & 타입 추론 (0) | 2023.01.17 |
[ TypeScript ] Interface & Intersection Type (0) | 2023.01.17 |
[ TypeScript ] Union 타입 & Type Alias (사용자 정의 타입) (0) | 2023.01.16 |
댓글