global+yarn

371 阅读1分钟

Node 中的模块化(2) -- global


  • 了解 global 的概念
Node 中的 全部成员 global
node 中虽然没有 window 全局对象,

在 Node 中,不存在全局作用域的概念,只有模块作用域的概念,
Node 中每个文件就是一个模块,即一个文件就是一个模块,
在这个模块内部定义的对象、属性以及方法,都属于当前的文件模块,外部无法访问

但是有一个类似于 window 的对象,叫 global 对象
可以使用 global 对象,在多个文件模块之间进行共享对象、属性以及方法

global 是 Node 中顶级作用域,
注意在浏览器中,顶层作用域是全局作用域。 
在 Node.js 中, 顶层作用域不是全局作用域,作用域只在该模块内

但是容易造成元素覆盖,一般不推荐在 node 中使用 global 进行元素的导出

yarn 的基本使用


  • 熟练掌握 yarn 的使用
一、安装yarn
npm install -g yarn
二、yarn 常用的命令
1. 初始化包
    npm init
    yarn init
    
2. 安装包
    npm install xxx --save
    yarn add xxx
    
3. 移除包
    npm uninstall xxx
    yarn remove xxx
    
4 .更新包
    npm update xxx
    yarn upgrade xxx
    
5. 安装开发依赖的包
    npm install xxx --save-dev
    yarn add xxx --dev
    
6. 全局安装
    npm install -g xxx
    yarn global add xxx
    
7. 安装所有依赖
    npm install
    yarn install
参考网址