我试图在Ubuntu 12.10 (Quantal Quetzal)上安装Node.js,但终端显示关于丢失包的错误。我试过这样做:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm

但是当我来到最后一行sudo apt-get install nodejs npm显示这个错误:

Failed to install some packages. This may mean that
you requested an impossible situation or if you are using the distribution
distribution that some required packages have not yet been created or been
been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
nodejs: Conflicts: npm
E: Failed to correct problems, you have held broken packages.

然后我卸载了ppa:chris-lea/node.js,我正在尝试第二种选择:

sudo apt-get install node.js
sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm

同样的错误,终端说npm是最新版本,但它也显示了我在顶部显示的文本。我认为问题是ppa:chris-lea/node.js,但我不知道如何解决它。


当前回答

获取最新的Node.js

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -

sudo apt-get install nodejs
node -v
npm -v

其他回答

下面是使用express生成器创建第一个程序的完整描述,

Ubuntu的包管理器

通过apt-get安装Node.js和npm,运行这些命令:

sudo apt-get update
sudo apt-get install nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo apt-get install npm

快速应用程序生成器:

$ npm install express-generator -g

使用-h选项显示命令选项:

$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help          output usage information
    -V, --version       output the version number
    -e, --ejs           add ejs engine support (defaults to jade)
        --hbs           add handlebars engine support
    -H, --hogan         add hogan.js engine support
    -c, --css <engine>  add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
        --git           add .gitignore
    -f, --force         force on non-empty directory

例如,下面在当前工作目录中创建一个名为myapp的Express应用程序:

$ express myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/public/javascripts
   create : myapp/public/images
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/users.js
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css
   create : myapp/views
   create : myapp/views/index.jade
   create : myapp/views/layout.jade
   create : myapp/views/error.jade
   create : myapp/bin
   create : myapp/bin/www

然后安装依赖项:

$ cd myapp
$ npm install

用下面的命令运行app:

$ DEBUG=myapp:* npm start

然后在浏览器中加载http://localhost:3000/以访问应用程序。

生成的应用程序具有以下目录结构:

├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

7 directories, 9 files

现在你可以简单地安装:

sudo apt-get install nodejs
sudo apt-get install npm

确保预先安装了Python和C解释器/编译器。如果没有,执行:

sudo apt-get install python g++ make

我个人是这样做的:

sudo apt-get install python g++ make
wget http://nodejs.org/dist/node-latest.tar.gz
tar xvfvz node-latest.tar.gz
cd node-v0.12.0
./configure
make
sudo make install

如果你想安装特定的版本,那么从Node.js站点下载你想要的版本,并执行最后的树步骤。

我强烈建议不要使用来自发行版市场的默认Node.js包,因为它可能已经过时了(例如,在Ubuntu市场上写这篇文章的时候,当前的版本是v0.10.25,与最新的版本(v0.12.0)相比,它太过时了)。

Node.js在目前支持的所有Ubuntu版本中都是一个snap包。对于Node.js,开发人员可以从一个或多个当前支持的版本中选择,并直接从NodeSource获得定期的自动更新。Node.js版本6、8、9、10、11、13、14、15、16、17和18目前可用,Snap Store会在Node.js发布后的几小时或几分钟内更新。

Node.js可以用一个命令来安装,例如:

sudo snap install node --classic --channel 11/stable

节点快照可以被命令节点访问,例如:

$ node -v
v11.5.0

最新版本的npm将作为节点快照的一部分安装。NPM应该在节点repl之外运行,在正常的shell中。安装节点snap后,运行以下命令启用npm更新检查:

sudo chown -R $USER:$(id -gn $USER) /home/<b>your-username</b>/.config

将上述命令中的your-username替换为您自己的用户名。然后执行npm -v检查npm版本是否为最新。作为一个例子,我检查了npm是最新的,用npm list yarn命令检查了一个已经安装的名为yarn的包的版本,然后用npm update yarn命令将现有的yarn包更新到最新版本

用户可以随时在Node.js的版本之间切换,而不需要涉及额外的工具,如nvm(节点版本管理器),例如:

sudo snap refresh node --channel=11/stable

用户可以通过切换来测试可以从最新边缘通道安装的Node.js的前沿版本:

sudo snap switch node --edge

这种方法只推荐给那些愿意参与上游测试和错误报告的用户。

Node.js LTS调度

Release Status Codename Initial release LTS Start Maintenance Start Maintenance End
6.x EOL Boron 2016-04-26 2016-10-18 2018-04-30 2019-04-30
7.x EOL 2017-05-30 2017-06-30
8.x EOL Carbon 2016-10-25 2017-10-31 2019-01-01 2019-12-31
9.x EOL 2017-10-01 2018-06-30
10.x EOL Dubnium 2018-04-24 2018-10-30 2020-05-19 2021-04-30
11.x EOL 2018-10-23 2019-06-01
12.x Maintenance LTS Erbium 2019-04-23 2019-10-21 2020-11-301 2022-04-30
13.x EOL 2019-10-22 2020-06-01
14.x Maintenance LTS Fermium 2020-04-21 2020-10-27 2021-10-30 2023-04-30
16.x Active LTS Gallium 2021-04-20 2021-10-26 2022-10-18 2024-04-30
17.x Current 2021-10-19 2022-04-01 2022-06-01
18.x Current 2022-04-19 2022-10-25 2023-10-18 2025-04-30

遵循NodeSource给出的说明,它致力于为Node.js创建一个可持续的生态系统。

对于Node.js >= 4。X

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_4.x | bash -
apt-get install -y nodejs