直接在node.js中使用browserify输出包 [英] using browserify output bundles directly in node.js

查看:105
本文介绍了直接在node.js中使用browserify输出包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以可以将为node.js编写的模块与browserify捆绑在一起。

Ok, so modules written for node.js can be combined into bundles with browserify.

但以防万一,我只有一堆 bundle 由browserify创建并且不是不是源,是否有可能在浏览器之外的node.js环境中要求或以其他方式使用这些捆绑包及其内容? (因为该代码对浏览器没有任何作用)

But just in case I only have a bunch of bundles created by browserify and not the source, would it still be possible to 'require' or otherwise use these bundles and their contents in a node.js environment besides the browser? (granted that the code does not do anything browser specific)

推荐答案


好,所以为可以使用
browserify将node.js组合成一个包。

Ok, so modules written for node.js can be combined into bundles with browserify.

首先,我不确定您的意思,因为browserify的创建是相反的。

Firstly I am not sure what you mean by this, as browserify was created to do the opposite. Browserify was made to allow the use of node's require() statements in the browser.


但为了以防万一,我只有一堆捆绑包,Browserify允许在浏览器中使用节点的require()语句。由browserify
创建,而不是由源创建,是否仍可能需要'require'或
,否则在浏览器之外的node.js
环境中使用这些捆绑包及其内容? (可以肯定的是,该代码不会对任何浏览器特定的功能执行
的操作)

But just in case I only have a bunch of bundles created by browserify and not the source, would it still be possible to 'require' or otherwise use these bundles and their contents in a node.js environment besides the browser? (granted that the code does not do anything browser specific)

是的,只要模块没有使用全局窗口范围,因为window在node.js中未定义。常见的帮助程序包(例如lodash,axios,moment,bluebird和q)都可以在node.js中使用。

Yes in short, as long as the modules do not use the global window scope because window is undefined in node.js. Common helper packages like lodash, axios, moment, bluebird, and q promises all work in node.js.

通常,通常对程序包进行修改,使其在两种浏览器中均能正常工作。和node.js。 package.json文件中有一个浏览器属性选项,可让您在发布npm模块时专门针对浏览器。通常,为浏览器设计的文件会缩小为一个文件,因为文件可能会被导入浏览器。这对于节点不是必需的,并且节点项目中可能有很多文件。

Generally though, packages are often modified to work in both the browser and node.js. There is a browser attribute option in package.json files that allows you to specifically target the browser when publishing npm modules. Often files designed for the browser are minified down to one file because of how files could potentially be imported into the browser. This is not necessary with node and there may be many files in a node project.

这篇关于直接在node.js中使用browserify输出包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆