nvmnodenpm 之间的区别
1. nvm 的官方叫法: nodejs 版本管理工具
nvm 相当于是家长, 一个家长可以管理多个孩子
也就是说: 一个 nvm 可以管理很多 node 版本和 npm 版本
2. nodejs
在项目开发时的所需要的代码库
3. npm
在安装的 nodejs 的时候, npm 也会跟着一起安装, 它是包管理工具
npm 管理 nodejs 中的第三方插件
nvmnodejsnpm 的关系:
nvm 是爸爸, 管理 nodejs 和 npm 这一对双胞胎兄弟 npm 是哥哥, npm 哥哥可以管理 node 弟弟的东西
个人是这么理解的, 要是有偏差, 请指点
mac 安装 nvm
打开 https://github.com/creationix/nvm 在下面的简介中找到 install 这几个字, 然后继续往下找, 直到找到
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
(随着时间变哈 , 这个版本可能会出现变化, 自行在 github 上查看)
复制这些字, 到 Mac 的 terminal 中, 就会安装 nvm, 安装完成后, 还暂时不能用, 需要复制它提示的两行代码 (就是下图拿箭头标出来的两行代码) 来配置环境变量:
完成以后 输入
nvm
出现
node version manger
说明 nvm 安装成功
mac 安装 node
如果大家之前做过 web 服务器的人都知道, nginx+lua 与现在流行的 node.js 都是可以做 web 服务器的, 前者在程序的写法和配置上要比后者麻烦, 但用起来都是差不多. 在这里建议大家如果对 lua 脚本语言不了解, 可以多了解这门脚本语言, 他号称是所有脚本语言执行效率是最高的一门脚本语言. 底层是基于 C 语言的, 非常好用, 跨平台!
下面我就来给大家配置一下 node.js 环境.
第一步: 打开终端, 输入以下命令安装 Homebrew
- ruby - e $(curl - fsSL https: //raw.githubusercontent.com/Homebrew/install/master/install);
第二步: 安装 node, 在终端输入以下命令
brew install node
第三步 查看 node 安装成功与否
node -v
以上三步 node 就安装成功了
程序测试
第一步: 新建一个文件 test.js
- var http = require(http); // 导入 Node.Js 中的 Http 的库文件, 并获取句柄
- //createServer 函数, 传入回调函数, request,response
- var server = http.createServer(function(req, res) {
- console.log("Get A Request...");
- res.writeHead(200, {
- "Content-Type": "Text/plain"
- });
- res.write("Hello NodeJs666");
- res.end();
- });
- server.listen(5000);
第二步: 用终端找到其所在的目录运行
node test.js
第三步: 通过浏览器进行访问 localhost:5000, 返回数据
第四步: 前端就可以通过调用这个接口进行数据解析了, 并且可以在当前页面进行数据展示了.
是不是很简单, 如果之前做过 web 服务接口开发的, 这个应该不会陌生, 甚至来说非常简单了.
npm 相关
npm install
安装本地包
npm install < package_name >
: 这个命令将在当前目录中创建 node_modules 目录(如果尚不存在), 并将该软件包下载到该目录该命令默认本地安装
安装了哪个版本的软件包?
如果本地目录中没有 package.json 文件, 则会安装最新版本的软件包
如果有 package.json 文件, 则安装满足该 package(如果有的话)在 package.json 中声明的 semver 规则的最新版本
安装全局包
npm install - g < package >
: 全局安装包
- package.json
- npm init
npm init: 这个命令用于创建一个 package.json
npm init --yes 或 npm init -y: 从当前目录中提取的信息生成默认的 package.json 创建过程中不会提问
如果您的目录中已经有一个 package.json 文件, 并且运行了 npm install, 那么 npm 将查看该文件中的 dependencies, 并下载满足所有这些的最新版本
package.json 文件中的 description 帮助人们在 npm 搜索中找到您的包, 所以在 package.json 中进行自定义描述非常有用
也可以完全自定义 package.json 文件的内容和在 init 期间提出的问题这通过创建自定义. npm-init.js 来完成默认情况下, npm 将查找您的主目录 ?/ .npm-init.js
dependencies 与 devDependencies
dependencies 和 devDependencies 指定了项目依赖的包
dependencies: 这些包在生产中需要
devDependencies: 这些包用于开发和测试
npm install < package_name > --save
命令会添加条目到 package.json 的 dependencies 中
npm install < package_name > --save - dev
命令会添加条目到 package.json 的 devDependencies 中
npm update
更新本地软件包
npm update: 用于更新依赖的软件包需要在 package.json 文件所在的目录中运行该命令
更新全局软件包
npm update - g < package >
: 更新全局软件包
npm update -g: 更新所有的全局软件包
npm outdated - g--depth = 0
: 找出需要更新的包
npm uninstall
卸载本地软件包
npm uninstall < package >
: 从 node_modules 目录中移除一个包
npm uninstall--save < package >
: 从 package.json 的 dependencies 中移除一个包
npm uninstall--save - dev < package >
: 从 package.json 的 devDependencies 中移除一个包
实际操作时, 发现使用
npm uninstall < package >
不仅会在 node_modules 目录下删除该包, 还会将该包在 package.json 中 dependencies 或 devDependencies 里面的信息删除
卸载全局软件包
npm uninstall - g < package >
: 卸载全局软件包
总结: 本地命令加上 - g 就是全局命令
参考自: npm
配置 eslint 在 pycharm 中
- I dont like to install things as a root user. If its possible to install something in my home directory, why not use it?
- I use PyCharm for programming and for example to validate JavaScript code it needs http://eslint.org package. Lets install ESLint in home directory and make it globally accessible.
- First, we need to tell NPM that it should install everything in ~/.npm directory.
- npm config set prefix~ / .npm
- Now every time when you install NPM package using -g option it will install it in ~/.npm directory. You dont need the root access to do it. All executables will be in ~/.npm/bin directory. For example if I install ESLint using
- npm install - g eslint
- command then it will be possible to run eslint command from terminal using full path:
- ~/.npm/bin/eslint
- Its inconvenient, right? Its better to be able to use just eslint command. To achieve that we need to modify ~/.bash_profilefile. Find in this file line which starts with PATH= and add ~/.npm/bin at the end of the line. In my ~/.bash_profile file it looks like this:
- PATH = $PATH: $HOME / .local / bin: $HOME / bin: ~ / .npm / bin
- If the line with PATH doesnt exist then you can add it:
- PATH = $PATH: ~ / .npm / bin
- Now you need to apply changes.
- source~ / .bash_profile
- And its done. You should be able to run eslint without specifying the full path to the file.
- Quick tip about PyCharm. PyCharm needs a full path to the ESLint directory, not to ESLint executable file. In settings you need to use
- ~ / .npm / lib / node_modules / eslint
- path.
- The error you are facing is because your configuration is not present. To configure the eslint type
- eslint --init
- then configure as your requirement.
- then execute the project again.
来源: http://www.bubuko.com/infodetail-2494266.html