如何使用npm安装requireJS用于浏览器 [英] How to use npm installed requireJS for browser
问题描述
虽然 requirejs
能够使用 npm-installed modules ,如果本身是通过 npm install requirejs
安装的话,如何首先使用 requirejs
?
While requirejs
is capable of using npm-installed modules, how do I use requirejs
in first place if itself is installed via npm install requirejs
?
我已经阅读了 requirejs 的示例html的#例子>示例-部分。他们似乎都假设将 require.js
下载到特定位置。由于文档明确说明
I have read examples of using requirejs
listed in the example-section. They all seems to assume require.js
is downloaded into a specific location. Since the documentation specifically said
不要做需要的事情(./ node_modules / foo / foo)。
do not do something like require("./node_modules/foo/foo").
我猜它不对放入 index.html
类似于:
I guess it is not right to put in index.html
something like:
<html>
<head>
<script data-main="scripts" src="node_modules/requirejs/require.js"></script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
如果是npm-installed,建议使用requirejs的方法是什么?如果我遗漏了文档中的内容,请告诉我。谢谢
What is the recommended way to use requirejs if it is npm-installed? If I missed something from the documentation please let me know. Thank you
推荐答案
看起来你正在混淆RequireJS的一系列不同用法:
It looks like you are conflating a bunch of different uses of RequireJS:
-
如何在浏览器中使用通过Node安装的RequireJS?
How can you use a RequireJS installed through Node in the browser?
您可以安装它 npm install requirejs
,然后你的HTML文件有一个脚本
元素,指向 node_modules / requirejs / require.js
。正如您在代码段中显示的那样。这里的所有都是它的。这就是说,我不喜欢在我部署的内容中使用 node_modules
,所以我通常有我的构建过程副本 require.js
其他地方。
You can just install it with npm install requirejs
, and then you have your HTML file have a script
element that points to node_modules/requirejs/require.js
. Exactly as you show in your code snippet. That's all there is to it. This being said, I don't like to have node_modules
in what I deploy, so I typically have my build process copy require.js
elsewhere.
如何在Node中加载带有RequireJS的npm安装模块?
How can you load npm-installed modules with RequireJS in Node?
假设没有RequireJS,你可以通过 require('foo')
加载模块 foo
。您安装RequireJS并将其加载为 requirejs
。如何使用RequireJS加载 foo
?你可以做 requirejs('foo')
。只要RequireJS没有通过自己的配置找到它,它将作为最后的手段发出对Node自己的 require
的调用,并将以这种方式加载它?这是一个例子。使用 npm install requirejs
安装RequireJS。创建此文件:
Suppose without RequireJS you would load the module foo
by doing require('foo')
. You install RequireJS and load it as requirejs
. How do you load foo
using RequireJS? You can just do requirejs('foo')
. So long as RequireJS does not find it through its own configuration, it will issue as a last resort a call to Node's own require
, and will load it this way? Here's an illustration. Install RequireJS with npm install requirejs
. Create this file:
var requirejs = require("requirejs");
var fs = requirejs("fs");
console.log(fs);
然后运行它。你将进入控制台Node的 fs
模块。
Then run it. You'll get on the console Node's fs
module.
如何加载npm安装的模块在浏览器中使用RequireJS?
How can you load npm-installed modules with RequireJS in a browser?
这取决于模块。 RequireJS不包含能够神奇地使npm安装的模块工作的代码浏览器。它最终取决于模块的结构。有些情况:
It depends on the modules. RequireJS does not contain code that will magically make a npm-installed module work in the browser. It ultimately depends on how the modules are structured. Some cases:
A。一些npm安装的模块可以使用RequireJS加载而无需修改。我创作的一个库是通过npm分发的,但却是AMD模块的集合。在浏览器中使用RequireJS加载它们是微不足道的。
A. Some npm-installed modules can be loaded with RequireJS without modification. There's one library I've authored which is distributed through npm and yet is a collection of AMD modules. It is trivial to load them with RequireJS in the browser.
B.它可能需要包含在 define
调用中。我最近加载了 merge-options
在我的一个项目中使用 gulp-wrap-amd
。 merge-options
是一个CommonJS模块。它不支持RequireJS开箱即用,但是如果用 define
调用它来包装它,它将起作用。
B. It may require being wrapped in define
calls. I've recently loaded merge-options
in one of my projects with gulp-wrap-amd
. merge-options
is a CommonJS module. It does not support RequireJS out-of-the-box but if you wrap it with a define
call, it will work.
C.在将其加载到浏览器中之前,可能需要更复杂的东西。例如,如果模块依赖于Node的 fs
模块,则必须提供运行的 fs
的替代品在浏览器中。它可能会为您的代码提供一个虚假的文件系统。
C. It may require something more complex before it will be loaded in a browser. For instance, if a module relies on Node's fs
module, you'll have to provide a replacement for fs
that runs in a browser. It will presumably present a fake filesystem to your code.
这篇关于如何使用npm安装requireJS用于浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!