无法在 ubuntu 上运行 nodemon (SyntaxError: Unexpected token)

las*_*ser 2 node.js express nodemon

我正在运行的命令:nodemon server.js,我收到错误:

nodemon server.js
/usr/local/lib/node_modules/nodemon/node_modules/update-notifier/index.js:58
            } catch {
                    ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/nodemon/bin/nodemon.js:15:3)

Run Code Online (Sandbox Code Playgroud)

我尝试了在 Ubuntu 上安装 nodemon 的各种方法,但每次运行时仍然遇到完全相同的错误。

Ron*_*hah 5

原因之一可能是您的 Node.js 安装已过时。您应该升级到最新版本的 node.js 您可以使用以下命令检查版本:

node -v
Run Code Online (Sandbox Code Playgroud)

如果版本是最新的,那么您应该尝试nodemon使用此命令进行全局安装

# using npm:
npm install -g nodemon 

# or using yarn: 
yarn global add nodemon
Run Code Online (Sandbox Code Playgroud)

如果您陷入旧版本的node.js,请执行以下操作:

sudo apt-get remove nodejs
sudo apt-get remove npm
Run Code Online (Sandbox Code Playgroud)

然后转到/etc/apt/sources.list.d并删除任何节点列表(如果有)。然后做一个

sudo apt-get update
Run Code Online (Sandbox Code Playgroud)

检查您的主文件夹中是否有任何.npm.node文件夹并将其删除。

如果您输入

which node
Run Code Online (Sandbox Code Playgroud)

你可以看到节点的位置。which nodejs也尝试一下which npm

然后像这样进行全新安装

cd ~
curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
nano nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs
Run Code Online (Sandbox Code Playgroud)