Tag: commonjs

Node.js – 模块是否只初始化一次?

我正在使用node-mysql驱动程序的node.js应用程序。 我不需要反复为每个模型模块设置mysql连接,我这样做: // DB.js var Client = require('mysql').Client; var DB_NAME = 'test_db'; var client = new Client(); client.user = 'user'; client.password = 'pass'; client.connect(); client.query('USE '+DB_NAME); module.exports = client; // in User.js var db = require("./DB"); // and make calls like: db.query(query, callback); 现在,我注意到DB.js只用DB连接初始化一次。 所以,随后相同的client对象正在使用…我如何构造DB.js,以便当我需要从模型,每次一个新的数据库连接将build立? 我知道这跟使用new东西有关,但是我无法把头围绕在里面。

Node.js ES6如何从模块中导出类?

我试图从Node.js 6.2.0中的CommonJS模块中导出一个ES6类 class MyClass{ //class contents here } exports = MyClass; 然后将其导入另一个模块中: var MyClass = require('/path/to/module.js') var instance = new MyClass(); 但是,我收到以下exception: TypeError: MyClass is not a constructor 我该如何正确地做到这一点? 请注意,我没有使用Babel / Tranceur,它是纯粹的JS,在最新的Node 6.2.0中实现,根据Kangax,93%实现了ES6。 //编辑:这不是出口vs module.exports的问题。 单独使用导出时,我得到一些与__proto__设置的对象。

CoffeeScript和NodeJS:如何导出多个类?

我想出口一些类,比如Dog和Cat 。 解决这个问题的方法之一是: class Dog bark: -> console.log "Arff! :D" class Cat meaow: -> console.log "Meaw!" module.exports = {Dog, Cat} 我怎样才能做类似的事情,而不用input两次类名呢?

漂亮打印HTML模块?

我正在处理一个grunt构build文件,命中一个URL并将输出写入一个静态的HTML文件。 我打的URL有压缩的HTML,我想漂亮的打印它,然后写入静态文件。 有没有好的模块来做到这一点? 我环顾四周,似乎Max Ogden的漂亮打印机是我最亲密的select(https://github.com/maxogden/commonjs-html-prettyprinter)。 也许如果我把它与grunt-shell任务结合起来呢? 真的,我宁愿只需要一个模块在咕噜,说漂亮(my-file.html),然后用fs来写,但到目前为止,这是难以捉摸的。

如何使WebStorm解决模块是function?

WebStorm在parsing从CommonJS模块返回的函数(并读取与之关联的JsDoc)方面做了很好的工作,例如: // utils/valid.js /** * Returns true no matter what. * @param {HTMLElement} element * @return {boolean} */ function isValid(element) { return true; } module.exports.isValid = isValid; // exports property 然后在代码完成和内联文档机制中正确提供这样的function时,在另一个文件中需要这样的模块。 // main.js var isValid = require('./utils/isValid').isValid; // works well 但是,如果函数直接作为模块导出返回,则会失败 // utils/valid.js module.exports = isValid; // exports object is a function 所以当需要这样一个模块时,WebStorm似乎并不知道它是什么: // main.js […]

Webpack无法加载我的npm模块

我有麻烦了,我只是做了npm卸载react-bootstrap,然后npm安装react-bootstrap和webpack无法加载这个模块了。 我像这样启动webpack: /var/www/cloud/node_modules/.bin/webpack –config webpack.config.js –watch –display-error-details 这里是错误,它只是无法find在react-bootstrap内的package.json: ERROR in ./core/static/core/js/modules/dashboard/admin/Customer.js Module not found: Error: Cannot resolve module 'react-bootstrap' in /var/www/cloud/core/static/core/js/modules/dashboard/admin resolve module react-bootstrap in /var/www/cloud/core/static/core/js/modules/dashboard/admin looking for modules in /var/www/cloud/node_modules resolve 'file' react-bootstrap in /var/www/cloud/node_modules resolve file /var/www/cloud/node_modules/react-bootstrap is not a file /var/www/cloud/node_modules/react-bootstrap.js doesn't exist /var/www/cloud/node_modules/react-bootstrap.jsx doesn't exist resolve 'file' or 'directory' /var/www/cloud/node_modules/react-bootstrap resolve […]

如何使用Rollup.js捆绑使用Sinon.js的testing?

我使用Rollup.js和插件rollup-plugin-node-resolve和rollup-plugin-commonjs来捆绑我使用Sinon.js的testing。 当我尝试运行捆绑文件时,出现以下错误: 错误:rollup-plugin-commonjs当前不支持dynamic需求 有没有任何解决这个错误,或者我必须使用一些其他工具,如Webpack?

从同一个文件中获取module.exports

在一个文件中我有这样的代码: module.exports.greet = function() {…} 我想在同一个文件中使用这个函数。 我认为这将工作: this.greet() 但事实并非如此。 什么是我必须使用的参考?

从几个TypeScript类创build一个CommonJS模块

我试图找出将我的应用程序分割成几个可供其他应用程序使用的CommonJS模块的最佳方法。 我有5个TS类,我想把它们作为一个CommonJS模块捆绑在一起。 然后我打算把这个模块发布到一个私有的NPM库,这样它可以被其他应用程序使用。 理想情况下,我想打包相关的* .d.ts定义文件。 什么是最好的方法来做到这一点? 我正在使用外部TS模块,但是这些模块为每个TS类生成一个单独的CommonJS模块。

CommonJS模块在哪里?

有时候我会听说CommonJS http://www.commonjs.org/是为了创build一组模块化javascript组件,但坦率地说,我从来没有理解任何东西。 这些模块化组件在哪里可以使用? 我没有看到太多的主页。