【node】(202405) linux下通过nvm安装node

环境

1
2
3
[root@ubuntu-22 ~]# uname -a
Linux ubuntu-22 6.5.0-34-generic #34~22.04.2-Ubuntu SMP PREEMPT_DYNAMIC Fri Apr 19 13:57:24 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

报错

1
2
3
4
[root@ubuntu-22 ~]# npx create-react-app my-app
You are running Node 12.22.9.
Create React App requires Node 14 or higher.
Please update your version of Node.

安装环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@ubuntu-22 ~]# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13527 100 13527 0 0 21473 0 --:--:-- --:--:-- --:--:-- 21505
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 290, done.
remote: Counting objects: 100% (290/290), done.
remote: Compressing objects: 100% (257/257), done.
remote: Total 290 (delta 35), reused 105 (delta 20), pack-reused 0
Receiving objects: 100% (290/290), 163.31 KiB | 720.00 KiB/s, done.
Resolving deltas: 100% (35/35), done.
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> 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
[root@ubuntu-22 ~]#

注意:需要重新打开一个shell,查看是否安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@ubuntu-22 ~]# nvm -v

Node Version Manager (v0.35.3)

Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`

Any options that produce colorized output should respect the `--no-colors` option.

Usage:
nvm --help Show this message
nvm --version Print out the installed version of nvm
nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
--skip-default-packages When installing, skip the default-packages file if it exists
--latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
--no-progress Disable the progress bar on any downloads
nvm uninstall <version> Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [--silent] <version> Modify PATH to use <version>. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [--silent] <version> [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version of Node
nvm ls [<version>] List installed versions, matching a given <version> if provided
--no-colors Suppress colored output
--no-alias Suppress `nvm alias` output
nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided
--lts When listing, only show LTS (long-term support) versions
--lts=<LTS name> When listing, only show versions for a specific LTS line
--no-colors Suppress colored output
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
nvm alias [<pattern>] Show all aliases beginning with <pattern>
--no-colors Suppress colored output
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm install-latest-npm Attempt to upgrade to the latest working `npm` on the current node version
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [current | <version>] Display path to installed node version. Uses .nvmrc if available
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm

Example:
nvm install 8.0.0 Install a specific version number
nvm use 8.0 Use the latest available 8.0.x release
nvm run 6.10.3 app.js Run app.js using node 6.10.3
nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3
nvm alias default 8.1.0 Set default node version on a shell
nvm alias default node Always default to the latest available node version on a shell

Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
[root@ubuntu-22 ~]# nvm ls

-> system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

[root@ubuntu-22 ~]# nvm ls-remote |grep LTS
v4.2.0 (LTS: Argon)
v4.2.1 (LTS: Argon)
v4.2.2 (LTS: Argon)
v4.2.3 (LTS: Argon)
v4.2.4 (LTS: Argon)
v4.2.5 (LTS: Argon)
v4.2.6 (LTS: Argon)
v4.3.0 (LTS: Argon)
v4.3.1 (LTS: Argon)
v4.3.2 (LTS: Argon)
v4.4.0 (LTS: Argon)
v4.4.1 (LTS: Argon)
v4.4.2 (LTS: Argon)
v4.4.3 (LTS: Argon)
v4.4.4 (LTS: Argon)
v4.4.5 (LTS: Argon)
v4.4.6 (LTS: Argon)
v4.4.7 (LTS: Argon)
v4.5.0 (LTS: Argon)
v4.6.0 (LTS: Argon)
v4.6.1 (LTS: Argon)
v4.6.2 (LTS: Argon)
v4.7.0 (LTS: Argon)
v4.7.1 (LTS: Argon)
v4.7.2 (LTS: Argon)
v4.7.3 (LTS: Argon)
v4.8.0 (LTS: Argon)
v4.8.1 (LTS: Argon)
v4.8.2 (LTS: Argon)
v4.8.3 (LTS: Argon)
v4.8.4 (LTS: Argon)
v4.8.5 (LTS: Argon)
v4.8.6 (LTS: Argon)
v4.8.7 (LTS: Argon)
v4.9.0 (LTS: Argon)
v4.9.1 (Latest LTS: Argon)
v6.9.0 (LTS: Boron)
v6.9.1 (LTS: Boron)
v6.9.2 (LTS: Boron)
v6.9.3 (LTS: Boron)
v6.9.4 (LTS: Boron)
v6.9.5 (LTS: Boron)
v6.10.0 (LTS: Boron)
v6.10.1 (LTS: Boron)
v6.10.2 (LTS: Boron)
v6.10.3 (LTS: Boron)
v6.11.0 (LTS: Boron)
v6.11.1 (LTS: Boron)
v6.11.2 (LTS: Boron)
v6.11.3 (LTS: Boron)
v6.11.4 (LTS: Boron)
v6.11.5 (LTS: Boron)
v6.12.0 (LTS: Boron)
v6.12.1 (LTS: Boron)
v6.12.2 (LTS: Boron)
v6.12.3 (LTS: Boron)
v6.13.0 (LTS: Boron)
v6.13.1 (LTS: Boron)
v6.14.0 (LTS: Boron)
v6.14.1 (LTS: Boron)
v6.14.2 (LTS: Boron)
v6.14.3 (LTS: Boron)
v6.14.4 (LTS: Boron)
v6.15.0 (LTS: Boron)
v6.15.1 (LTS: Boron)
v6.16.0 (LTS: Boron)
v6.17.0 (LTS: Boron)
v6.17.1 (Latest LTS: Boron)
v8.9.0 (LTS: Carbon)
v8.9.1 (LTS: Carbon)
v8.9.2 (LTS: Carbon)
v8.9.3 (LTS: Carbon)
v8.9.4 (LTS: Carbon)
v8.10.0 (LTS: Carbon)
v8.11.0 (LTS: Carbon)
v8.11.1 (LTS: Carbon)
v8.11.2 (LTS: Carbon)
v8.11.3 (LTS: Carbon)
v8.11.4 (LTS: Carbon)
v8.12.0 (LTS: Carbon)
v8.13.0 (LTS: Carbon)
v8.14.0 (LTS: Carbon)
v8.14.1 (LTS: Carbon)
v8.15.0 (LTS: Carbon)
v8.15.1 (LTS: Carbon)
v8.16.0 (LTS: Carbon)
v8.16.1 (LTS: Carbon)
v8.16.2 (LTS: Carbon)
v8.17.0 (Latest LTS: Carbon)
v10.13.0 (LTS: Dubnium)
v10.14.0 (LTS: Dubnium)
v10.14.1 (LTS: Dubnium)
v10.14.2 (LTS: Dubnium)
v10.15.0 (LTS: Dubnium)
v10.15.1 (LTS: Dubnium)
v10.15.2 (LTS: Dubnium)
v10.15.3 (LTS: Dubnium)
v10.16.0 (LTS: Dubnium)
v10.16.1 (LTS: Dubnium)
v10.16.2 (LTS: Dubnium)
v10.16.3 (LTS: Dubnium)
v10.17.0 (LTS: Dubnium)
v10.18.0 (LTS: Dubnium)
v10.18.1 (LTS: Dubnium)
v10.19.0 (LTS: Dubnium)
v10.20.0 (LTS: Dubnium)
v10.20.1 (LTS: Dubnium)
v10.21.0 (LTS: Dubnium)
v10.22.0 (LTS: Dubnium)
v10.22.1 (LTS: Dubnium)
v10.23.0 (LTS: Dubnium)
v10.23.1 (LTS: Dubnium)
v10.23.2 (LTS: Dubnium)
v10.23.3 (LTS: Dubnium)
v10.24.0 (LTS: Dubnium)
v10.24.1 (Latest LTS: Dubnium)
v12.13.0 (LTS: Erbium)
v12.13.1 (LTS: Erbium)
v12.14.0 (LTS: Erbium)
v12.14.1 (LTS: Erbium)
v12.15.0 (LTS: Erbium)
v12.16.0 (LTS: Erbium)
v12.16.1 (LTS: Erbium)
v12.16.2 (LTS: Erbium)
v12.16.3 (LTS: Erbium)
v12.17.0 (LTS: Erbium)
v12.18.0 (LTS: Erbium)
v12.18.1 (LTS: Erbium)
v12.18.2 (LTS: Erbium)
v12.18.3 (LTS: Erbium)
v12.18.4 (LTS: Erbium)
v12.19.0 (LTS: Erbium)
v12.19.1 (LTS: Erbium)
v12.20.0 (LTS: Erbium)
v12.20.1 (LTS: Erbium)
v12.20.2 (LTS: Erbium)
v12.21.0 (LTS: Erbium)
v12.22.0 (LTS: Erbium)
v12.22.1 (LTS: Erbium)
v12.22.2 (LTS: Erbium)
v12.22.3 (LTS: Erbium)
v12.22.4 (LTS: Erbium)
v12.22.5 (LTS: Erbium)
v12.22.6 (LTS: Erbium)
v12.22.7 (LTS: Erbium)
v12.22.8 (LTS: Erbium)
v12.22.9 (LTS: Erbium)
v12.22.10 (LTS: Erbium)
v12.22.11 (LTS: Erbium)
v12.22.12 (Latest LTS: Erbium)
v14.15.0 (LTS: Fermium)
v14.15.1 (LTS: Fermium)
v14.15.2 (LTS: Fermium)
v14.15.3 (LTS: Fermium)
v14.15.4 (LTS: Fermium)
v14.15.5 (LTS: Fermium)
v14.16.0 (LTS: Fermium)
v14.16.1 (LTS: Fermium)
v14.17.0 (LTS: Fermium)
v14.17.1 (LTS: Fermium)
v14.17.2 (LTS: Fermium)
v14.17.3 (LTS: Fermium)
v14.17.4 (LTS: Fermium)
v14.17.5 (LTS: Fermium)
v14.17.6 (LTS: Fermium)
v14.18.0 (LTS: Fermium)
v14.18.1 (LTS: Fermium)
v14.18.2 (LTS: Fermium)
v14.18.3 (LTS: Fermium)
v14.19.0 (LTS: Fermium)
v14.19.1 (LTS: Fermium)
v14.19.2 (LTS: Fermium)
v14.19.3 (LTS: Fermium)
v14.20.0 (LTS: Fermium)
v14.20.1 (LTS: Fermium)
v14.21.0 (LTS: Fermium)
v14.21.1 (LTS: Fermium)
v14.21.2 (LTS: Fermium)
v14.21.3 (Latest LTS: Fermium)
v16.13.0 (LTS: Gallium)
v16.13.1 (LTS: Gallium)
v16.13.2 (LTS: Gallium)
v16.14.0 (LTS: Gallium)
v16.14.1 (LTS: Gallium)
v16.14.2 (LTS: Gallium)
v16.15.0 (LTS: Gallium)
v16.15.1 (LTS: Gallium)
v16.16.0 (LTS: Gallium)
v16.17.0 (LTS: Gallium)
v16.17.1 (LTS: Gallium)
v16.18.0 (LTS: Gallium)
v16.18.1 (LTS: Gallium)
v16.19.0 (LTS: Gallium)
v16.19.1 (LTS: Gallium)
v16.20.0 (LTS: Gallium)
v16.20.1 (LTS: Gallium)
v16.20.2 (Latest LTS: Gallium)
v18.12.0 (LTS: Hydrogen)
v18.12.1 (LTS: Hydrogen)
v18.13.0 (LTS: Hydrogen)
v18.14.0 (LTS: Hydrogen)
v18.14.1 (LTS: Hydrogen)
v18.14.2 (LTS: Hydrogen)
v18.15.0 (LTS: Hydrogen)
v18.16.0 (LTS: Hydrogen)
v18.16.1 (LTS: Hydrogen)
v18.17.0 (LTS: Hydrogen)
v18.17.1 (LTS: Hydrogen)
v18.18.0 (LTS: Hydrogen)
v18.18.1 (LTS: Hydrogen)
v18.18.2 (LTS: Hydrogen)
v18.19.0 (LTS: Hydrogen)
v18.19.1 (LTS: Hydrogen)
v18.20.0 (LTS: Hydrogen)
v18.20.1 (LTS: Hydrogen)
v18.20.2 (Latest LTS: Hydrogen)
v20.9.0 (LTS: Iron)
v20.10.0 (LTS: Iron)
v20.11.0 (LTS: Iron)
v20.11.1 (LTS: Iron)
v20.12.0 (LTS: Iron)
v20.12.1 (LTS: Iron)
v20.12.2 (LTS: Iron)
v20.13.0 (Latest LTS: Iron)
[root@ubuntu-22 ~]#


[root@ubuntu-22 ~]# nvm install v20.13.0
Downloading and installing node v20.13.0...
Downloading https://nodejs.org/dist/v20.13.0/node-v20.13.0-linux-x64.tar.xz...
################################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.13.0 (npm v10.5.2)
Creating default alias: default -> v20.13.0
[root@ubuntu-22 ~]#


[root@ubuntu-22 ~]# nvm ls
-> v20.13.0
system
default -> v20.13.0
node -> stable (-> v20.13.0) (default)
stable -> 20.13 (-> v20.13.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/iron (-> v20.13.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.2 (-> N/A)
lts/iron -> v20.13.0

开始安装,贼慢

1
2
3
4
5
6
7
8
9
[root@ubuntu-22 ~]# npx create-react-app my-app

Creating a new React app in /root/my-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...

(#########⠂⠂⠂⠂⠂⠂⠂⠂⠂) ⠋ idealTree:@babel/core: sill placeDep ROOT semver@7.6.1 OK for: react-scripts@5.0.1 want: ^7.3.5

换源

1
2
3
4
5
6
7
8
9
# 原始
[root@ubuntu-22 ~]# npm config get registry
https://registry.npmjs.org/


# 换源
[root@ubuntu-22 ~]# npm config set registry https://registry.npm.taobao.org
[root@ubuntu-22 ~]# npm config get registry
https://registry.npm.taobao.org

重新安装,报错证书问题

1
2
3
4
5
6
7
[root@ubuntu-22 ~]# npx create-react-app my-app
npm ERR! code CERT_HAS_EXPIRED
npm ERR! errno CERT_HAS_EXPIRED
npm ERR! request to https://registry.npm.taobao.org/create-react-app failed, reason: certificate has expired

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2024-05-08T15_23_38_989Z-debug-0.log
[root@ubuntu-22 ~]#

原因:设置的淘宝镜像过期了

重新换源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@ubuntu-22 ~]# npm config set registry https://registry.npmmirror.com
[root@ubuntu-22 ~]#
[root@ubuntu-22 ~]# npm config get registry
https://registry.npmmirror.com
[root@ubuntu-22 ~]#
[root@ubuntu-22 ~]# npm config list
; "user" config from /root/.npmrc

registry = "https://registry.npmmirror.com"

; node bin location = /root/.nvm/versions/node/v20.13.0/bin/node
; node version = v20.13.0
; npm local prefix = /root
; npm version = 10.5.2
; cwd = /root
; HOME = /root
; Run `npm config ls -l` to show all defaults.
[root@ubuntu-22 ~]#

再次安装,成功~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@ubuntu-22 ~]# npx create-react-app my-app
Need to install the following packages:
create-react-app@5.0.1
Ok to proceed? (y) y

Creating a new React app in /root/my-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...


added 1489 packages in 2m

258 packages are looking for funding
run `npm fund` for details

Initialized a git repository.

Installing template dependencies using npm...

added 67 packages, and changed 1 package in 10s

262 packages are looking for funding
run `npm fund` for details
Removing template package using npm...


removed 1 package in 4s

262 packages are looking for funding
run `npm fund` for details

Created git commit.

Success! Created my-app at /root/my-app
Inside that directory, you can run several commands:

npm start
Starts the development server.

npm run build
Bundles the app into static files for production.

npm test
Starts the test runner.

npm run eject
Removes this tool and copies build dependencies, configuration files
and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

cd my-app
npm start

Happy hacking!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ubuntu-22 my-app]# npm start
...


Compiled successfully!

You can now view my-app in the browser.

http://localhost:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

webpack compiled successfully

然后会自动跳转到 http://localhost:3000/

参考

Node.js 开发环境安装之 Linux
npm报错:request to https://registry.npm.taobao.org failed, reason certificate has expired


【node】(202405) linux下通过nvm安装node
http://example.com/2024/05/10/software/【node】(202405) linux下通过nvm安装node/
作者
ningan123
发布于
2024年5月10日
许可协议