commonjs

CommonJs

巧了我就是萌 提交于 2020-02-09 22:02:16
特点 每个文件被视为一个独立的模块 关键字 exports , module , require exports exports 变量是在模块的文件级别作用域内有效的,它在模块被执行前被赋予 module.exports 的值。 它有一个快捷方式,以便 module.exports.f = … 可以被更简洁地写成 exports.f = … 注意,就像任何变量,如果一个新的值被赋值给 exports,它就不再绑定到 module.exports: 12 module.exports.hello = true; exports = { hello: false }; // 不导出,只在模块内有效 Node导出模块常用方式 到处命名空间 12345 const fs = require('fs');fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('成功删除 /tmp/hello');}); 导出函数 123456 大专栏 CommonJs7 //express.jsmodule.exports = function(){ //some code here};//app.jsconst express = require('./express');const app = express();

CommonJS和AMD/CMD

孤街浪徒 提交于 2020-01-28 14:10:33
先回答我:为什么模块很重要? 答:因为有了模块,我们就可以更方便地使用别人的代码,想要什么功能,就加载什么模块。 但是,这样做有一个前提,那就是大家必须以同样的方式编写模块,否则你有你的写法,我有我的写法,岂不是乱了套! 于是下面三个模块规范出来了,这篇文章也出来了(拼出来的 {捂脸笑})。 JS中的模块规范(CommonJS,AMD,CMD),如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已。 现在就看看吧,这些规范到底是啥东西,干嘛的。本文包括这三个规范的来源及对应的产物的原理。 一、CommonJS 1.一开始大家都认为JS是辣鸡,没什么用,官方定义的API只能构建基于浏览器的应用程序,逗我呢,这太狭隘了吧(用了个高端词,嘎嘎),CommonJS就按耐不住了,CommonJS API定义很多普通应用程序(主要指非浏览器的应用)使用的API,从而填补了这个空白。它的终极目标是提供一个类似Python,Ruby和Java标准库。这样的话,开发者可以使用CommonJS API编写应用程序,然后这些应用可以运行在不同的JavaScript解释器和不同的主机环境中。 在兼容CommonJS的系统中,你可以使用JavaScript开发以下程序: (1).服务器端JavaScript应用程序 (2)

commonJS — 浏览器操作(for Browser)

不羁岁月 提交于 2020-01-25 22:33:26
for Browser github: https://github.com/laixiangran/commonJS/blob/master/src/forBrowser.js 代码 /** * Created by laixiangran on 2016/1/24 * homepage:http://www.cnblogs.com/laixiangran/ * for Browser */ (function(undefined) { var com = window.COM = window.COM || {}; var $B = com.$B = {}; // 呈现引擎信息 var engine = { // 呈现引擎 ie: 0, gecko: 0, webkit: 0, khtml: 0, opera:0, //具体版本号 ver: null }; var browser = { // 浏览器 ie: 0, edge: 0, firefox: 0, safari: 0, konq: 0, opera: 0, chrome: 0, // 具体版本号 ver: null }; // 平台、设备和操作系统 var system = { win: false, mac: false, unix: false, // 移动设备 iphone: false, ipod: false

Webpack学习笔记——CommonJS与ES6 Module的区别之动态与静态和值拷贝与动态映射

北战南征 提交于 2020-01-25 18:57:43
动态与静态 CommonJS与ES6 Module最本质的区别在于CommonJS对模块依赖的解决是“动态的”而ES6 Module是“静态的”。在这里“动态的”含义是, 模块依赖关系的建立发生在代码运行阶段 ;而“静态”则是 模块依赖关系的建立发生在代码编译阶段 接下来举例说明一下 commonJS例子 B文件 //calculator.js module.exports={name:"calculator"}; A文件 //index.js const name=require('./calculator.js').name; 当模块A加载模块B时,会执行B中的代码,并将其module.exports对象作为require函数的返回值进行返回。并且require的模块路径可以动态指定,并支持传入一个表达式或者一个if进行判断是否加载模块。因此可以看出,在commonJS模块被执行前,并没有办法确定明确的依赖关系,模块的导入、导出发生在代码的运行阶段。 ES6 Module例子 //calculator.js export const name='calculator'; //index.js import {name} from './calculator.js'; ES6 Module的导入、导出语句都是声明式的,它不支持导入的路径是一个表达式,并且导入

TypeScript won't resolve external module (node.js)

China☆狼群 提交于 2020-01-23 05:52:05
问题 I would like to use moment.js in my node application, so I installed moment.js using node's package manager npm: npm install moment@2.4.0 Just to be on the safe side, I checked moment is not installed globally and the installed version is really version 2.4.0 (version 2.4.0 in order to use the correct d.ts file ...) require("moment").version Alright, seems to be good. I'm also using the latest version of TypeScript (0.9.5). So, now I added the following file to my projects root directory

Browserify with jQuery >= 2 produces “jQuery requires a window with a document”

北慕城南 提交于 2020-01-22 09:46:22
问题 I'm using browserify to bundle my front-end javascript using CommonJS-style dependencies. For example, I have: $ = require('jquery/dist/jquery'); // v2.1.0-beta2 _ = require('underscore'); Backbone = require('backbone'); However, when browserify bundles the dependencies I run into the following console error: Error: jQuery requires a window with a document Looking at the jQuery code, I see it's trying to use this for the global window . (function( window, factory ) { .... }(this, function(

Node解析之----模块机制篇

元气小坏坏 提交于 2020-01-16 07:28:49
开篇前,我们先来看张图, 看node与W3C组织、CommonJS组织、ECMAScript之间的关系。 Node借鉴来CommonJS的Modules规范实现了一套非常易用的模块系统,NPM对Packages规范 的完好支持使得Node应用在开发过程中事半功倍。 一、CommonJS 的模块规范 CommonJS中的大部分规范涵盖了模块、二进制、Buffer、字符集编码、I/O流、进程环境、文件系统、套接字、单元测试、Web服务器网关接口、包管理等。 1.1 模块引用 模块示例代码如下: var math = require('math'); 在CommonJS规范中,存在require()方法,这个方法接受模块标识,以此引入一个模块的API到当前上下文中。 1.2模块定义 在模块中,上下文提供require()方法引入外部模块。对应引入的功能,上下文提供了exports对象用于导出当前模块的方法或者变量,并且它是唯一导出的出口。 例如: // math.js exports.add = function () { var sum = 0, i = 0, args = arguments, l = args.length; while (i < l) { sum += args[i++]; } return sum; }; // program.js var math =

CommonJS和AMD

主宰稳场 提交于 2020-01-16 07:16:21
CommonJS中,有一个全局性方法require(),用于加载模块,适用于服务器端,同步加载,  var math = require('math');  math.add(2,3); // 5 这对服务器端不是一个问题,因为所有的模块都存放在本地硬盘,可以同步加载完成,等待时间就是硬盘的读取时间。但是,对于浏览器,这却是一个大问题,因为模块都放在服务器端,等待时间取决于网速的快慢,可能要等很长时间,浏览器处于"假死"状态。 AMD也采用require()语句加载模块,但是不同于CommonJS,它要求两个参数: require([module], callback); 只要通过require.js加载 来源: https://www.cnblogs.com/Ferrari/p/7205777.html

How to browserify a browserified module?

落花浮王杯 提交于 2020-01-16 03:48:06
问题 The title seems confusing but I'll give an example. Let's say I create a module that uses ES6 that runs in the browser, so I use browserify with babelify to build everything. Now I want to include that same module in a project that uses browserify, but does not uses Babel to compile ES6, so I need the compiled version. I tried to require the "browserified" module like this: // es5-project.js require('./compiled-module-with-browserify'); But when I run browserify es5-project.js I start to get

ES6 module's “import” officially compatible with CommonJS and AMD?

£可爱£侵袭症+ 提交于 2020-01-13 12:03:06
问题 From this article : https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ It is written that The new standard is designed to interoperate with existing CommonJS and AMD modules. And more precisely All CommonJS and AMD modules are presented to ES6 as having a default export If it is really the case all we'd need is a ES6 polyfill and we wouldn't have to do use anything else. Yet for eg this ES6 Polyfill :https://github.com/ModuleLoader/es6-module-loader doesn't seem to allow loading CommonJS