使用nvm安裝node與環境設定
使用 nvm 安裝 node
首先,如果你的 ubuntu 系統裡已經安裝了 nodejs,請先移除:
Uninstall Node.JS using Linux command line? - Stack Overflow
sudo apt-get remove nodejs然後安裝nvm
How to Update Node.js to Latest Version {Linux, Windows, and MacOS}
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bashsource ~/.bashrcnvm --version列出目前系統上安裝的node版本:
nvm ls v10.24.1
-> v16.14.0
default -> v16.14.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.14.0) (default)
stable -> 16.14 (-> v16.14.0) (default)
lts/* -> lts/gallium (-> v16.14.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
lts/erbium -> v12.22.10 (-> N/A)
lts/fermium -> v14.19.0 (-> N/A)
lts/gallium -> v16.14.0列出遠端所有的版本:
nvm ls-remote只列出 lts 版本:
nvm:安裝、切換不同 Node.js 版本的管理器 | Titangene Blog
nvm ls-remote --lts 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 (Latest LTS: Fermium)
v16.13.0 (LTS: Gallium)
v16.13.1 (LTS: Gallium)
v16.13.2 (LTS: Gallium)
-> v16.14.0 (Latest LTS: Gallium)安裝node
nvm install [version.number]npm doctor
檢查目前的環境配置是否正確
執行
npm doctor如果結果是這樣:
Check Value Recommendation/Notes
npm ping ok
npm -v ok current: v8.5.4, latest: v8.5.4
node -v ok current: v16.14.0, recommended: v16.14.0
npm config get registry ok using default registry (https://registry.npmjs.org/)
which git ok /usr/bin/git
Perms check on cached files ok
Perms check on local node_modules not ok Check the permissions of files in /home/allenyl/Project/sentencing-system/frontend/node_modules (should be owned by current user)
Perms check on global node_modules ok
Perms check on local bin folder ok
Perms check on global bin folder ok
Verify cache contents ok verified 663 tarballs
npm ERR! Some problems found. See above for recommendations.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/allenyl/.npm/_logs/2022-03-11T04_21_46_344Z-debug-0.log中間有一項是 permission 問題,多半是node_modules權限設置不對。
改owner
chown username:username -R node_modules改權限:
sh: 1: vue-cli-service: Permission denied · Issue #5210 · vuejs/vue-cli
chmod -R a+X node_moduleschmod -R a+rw node_modules
To read a file, its read permission needs to be set. However, to read a directory and the listing of its files, both the read and the execute permissions need to be set. If they aren't, you get weird errors like the ones you're experiencing.
To set the read permission on files and the read and execute permissions on directories recursively, use this command:
chmod -R a+rX directorynameHere's an explanation of that command:
chmodis the name of the command, use for changing the permissions of files.-Ris the recursive flag. It means apply this command to the directory, and all of its children, and of its children's children, and so on.astands for all: apply these permissions the owner of the file, the group owner of the file, and all other users.+means add the following permissions if they aren't set already.rmeans the read permission.Xmeans the execute permission, but only on directories. Lower-casexwould mean the execute permission on both files and directories.
最後檢查結果應該要都ok
Check Value Recommendation/Notes
npm ping ok
npm -v ok current: v8.5.4, latest: v8.5.4
node -v ok current: v16.14.0, recommended: v16.14.0
npm config get registry ok using default registry (https://registry.npmjs.org/)
which git ok /usr/bin/git
Perms check on cached files ok
Perms check on local node_modules ok
Perms check on global node_modules ok
Perms check on local bin folder ok
Perms check on global bin folder ok
Verify cache contents ok verified 663 tarballs解決 npm npm does not support Node.js
參考官網的版本對應 https://nodejs.org/zh-cn/download/releases/ ,安裝對應版本的node跟npm
設定 default node version
node.js - How can the default node version be set using NVM? - Stack Overflow
如果發現 node 版本跟 npm 不匹配,安裝了新版的node之後,可以使用以下指令來指定預設使用的node版本:
nvm alias default v16.14.0切換node版本
如果要暫時切換node版本,可輸入:
nvm use v10.6.3移除node版本
nvm uninstall v10.6.3參考:
安裝serve 套件
安裝:
global
npm install -g serve@latestlocal
npm install serve@latest移除:
global
npm uninstall -g servelocal
npm uninstall serve關於 package-lock.json
在 package.json 中,可以使用 semver 表示法设置要升级到的版本(补丁版本或次版本),例如:
- 如果写入的是
〜0.13.0,则只更新补丁版本:即0.13.1可以,但0.14.0不可以。- 如果写入的是
^0.13.0,则要更新补丁版本和次版本:即0.13.1、0.14.0、依此类推。- 如果写入的是
0.13.0,则始终使用确切的版本。当尝试使用
npm install命令在另一台机器上复制项目时,如果指定了〜语法并且软件包发布了补丁版本,则该软件包会被安装。^和次版本也一样。如果指定确切的版本,例如示例中的
0.13.0,则不会受到此问题的影响。可能是你,或者是其他人,会在某处尝试通过运行
npm install初始化项目。因此,原始的项目和新初始化的项目实际上是不同的。 即使补丁版本或次版本不应该引入重大的更改,但还是可能引入缺陷。
package-lock.json会固化当前安装的每个软件包的版本,当运行npm install时,npm会使用这些确切的版本。
package-lock.json文件需要被提交到 Git 仓库,以便被其他人获取(如果项目是公开的或有合作者,或者将 Git 作为部署源)。当运行
npm update时,package-lock.json文件中的依赖的版本会被更新。
运行任务
package.json 文件支持一种用于指定命令行任务(可通过使用以下方式运行)的格式:
npm run <task-name>例如:
{ "scripts": { "start-dev": "node lib/server-development", "start": "node lib/server-production" }, }使用此特性运行 Webpack 是很常见的:
{ "scripts": { "watch": "webpack --watch --progress --colors --config webpack.conf.js", "dev": "webpack --progress --colors --config webpack.conf.js", "prod": "NODE_ENV=production webpack -p --config webpack.conf.js", }, }因此可以运行如下,而不是输入那些容易忘记或输入错误的长命令:
$ npm run watch $ npm run dev $ npm run prod
npm安装package.json中的模块依赖 - 前端[色色] - 博客园
1.package.json不存在时
运行命令: npm init可自动创建package.json文件
npm install
安装单个软件包
也可以通过运行以下命令安装特定的软件包:
npm install <package-name>通常会在此命令中看到更多标志:
--save安装并添加条目到package.json文件的 dependencies。--save-dev安装并添加条目到package.json文件的 devDependencies。区别主要是,
devDependencies通常是开发的工具(例如测试的库),而dependencies则是与生产环境中的应用程序相关。
npm uninstall
若要卸载之前在本地安装(在
node_modules文件夹使用npm install <package-name>)的软件包,则从项目的根文件夹(包含node_modules文件夹的文件夹)中运行:npm uninstall <package-name>如果使用
-S或--save标志,则此操作还会移除package.json文件中的引用。如果程序包是开发依赖项(列出在
package.json文件的 devDependencies 中),则必须使用-D或--save-dev标志从文件中移除:npm uninstall -S <package-name> npm uninstall -D <package-name>如果该软件包是全局安装的,则需要添加
-g或--global标志:npm uninstall -g <package-name>例如:
npm uninstall -g webpack可以在系统上的任何位置运行此命令,因为当前所在的文件夹无关紧要。