본문 바로가기
문제해결

[AWS] SyntaxError: Unexpected token '.'해결 / EC2에 nodejs 원하는 버전 설치하기

by limew 2024. 1. 5.

express, react 프로젝트를 aws ec2 인스턴스에 연결한 뒤

ssh를 통해 aws서버에 접속하여 npm start 하는 과정에 다음과 같은 오류를 만났다.

 

 

이는 사용 중인 Node.js 버전에서 널 병합 연산자(??)를 지원하지 않아 발생하는 것으로
널 병합 연산자는 Node.js 14 버전 이상에서 지원되기 떄문에 node를 업그레이드 해야했다.

 

 

하지만 아래와 같이 새로운 버전을 설치해도 여전히 12버전이고 또한 14는 더이상 지원하지 않는?다는 warning이 떴다

$ sudo apt install nodejs
$ nodejs -v
v12.22.9

 

 

 

 

그러다 다음과 같은 해결책으로 node를 upgrade할 수 있었다.

 

1. nvm 설치하기

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

 

 

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15037  100 15037    0     0  53484      0 --:--:-- --:--:-- --:--:-- 53512
=> Downloading nvm from git to '/home/ubuntu/.nvm'
=> Cloning into '/home/ubuntu/.nvm'...
remote: Enumerating objects: 365, done.
remote: Counting objects: 100% (365/365), done.
remote: Compressing objects: 100% (314/314), done.
remote: Total 365 (delta 43), reused 160 (delta 25), pack-reused 0
Receiving objects: 100% (365/365), 364.78 KiB | 12.58 MiB/s, done.
Resolving deltas: 100% (43/43), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/ubuntu/.bashrc
=> Appending bash_completion source string to /home/ubuntu/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

/usr/local/lib
└── ts-node@10.9.2
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

 

 

2. nvm을 사용하기 위해 터미널을 다시 키거나 다음을 입력한다.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

 

 

3. 설치된 nvm버전을 확인한다

 

$ nvm --version
0.39.1

 

 

4. 원하는 버전의 node를 설치한다

$ nvm install 17
Downloading and installing node v17.9.1...
Downloading https://nodejs.org/dist/v17.9.1/node-v17.9.1-linux-x64.tar.xz...
########################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v17.9.1 (npm v8.11.0)
Creating default alias: default -> 17 (-> v17.9.1)

 

 

5. node버전을 확인한다

$ node -v
v17.9.1

 

 

reference