简体   繁体   English

如何在浏览器中使用 npm 安装的 requireJS

[英]How to use npm installed requireJS for browser

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能够使用npm 安装的模块,但如果requirejs是通过npm install requirejs安装的,我该如何首先使用它?

I have read examples of using requirejs listed in the example-section .我已经阅读了example-section 中列出的使用requirejs示例 They all seems to assume require.js is downloaded into a specific location.他们似乎都假设require.js已下载到特定位置。 Since the documentation specifically said由于文档特别指出

do not do something like require("./node_modules/foo/foo").不要做类似 require("./node_modules/foo/foo") 的事情。

I guess it is not right to put in index.html something like:我想在index.html放入如下内容是不对的:

<html>
    <head>
        <script data-main="scripts" src="node_modules/requirejs/require.js"></script>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

What is the recommended way to use requirejs if it is npm-installed?如果安装了 npm,推荐使用 requirejs 的方法是什么? If I missed something from the documentation please let me know.如果我错过了文档中的某些内容,请告诉我。 Thank you谢谢

It looks like you are conflating a bunch of different uses of RequireJS:看起来您正在混淆 RequireJS 的一系列不同用途:

  1. How can you use a RequireJS installed through Node in the browser?如何在浏览器中使用通过 Node 安装的 RequireJS?

    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 .您只需使用npm install requirejs安装它,然后您的 HTML 文件就有一个指向node_modules/requirejs/require.jsscript元素。 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_modules ,所以我通常在其他地方有我的构建过程副本require.js

  2. How can you load npm-installed modules with RequireJS in Node?如何在 Node 中使用 RequireJS 加载 npm 安装的模块?

    Suppose without RequireJS you would load the module foo by doing require('foo') .假设没有 RequireJS,您将通过执行require('foo')加载模块foo You install RequireJS and load it as requirejs .您安装 RequireJS 并将其加载为requirejs How do you load foo using RequireJS?你如何使用 RequireJS 加载foo You can just do requirejs('foo') .你可以只做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?只要 RequireJS 没有通过自己的配置找到它,它就会作为最后的手段发出对 Node 自己的require的调用,并以这种方式加载它? Here's an illustration.这是一个插图。 Install RequireJS with npm install requirejs .使用npm install requirejs安装 RequireJS。 Create this file:创建此文件:

     var requirejs = require("requirejs"); var fs = requirejs("fs"); console.log(fs);

    Then run it.然后运行它。 You'll get on the console Node's fs module.您将进入控制台节点的fs模块。

  3. 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. RequireJS 不包含可以神奇地使 npm 安装的模块在浏览器中工作的代码。 It ultimately depends on how the modules are structured.这最终取决于模块的结构。 Some cases:某些情况下:

    A. Some npm-installed modules can be loaded with RequireJS without modification. A. 某些 npm 安装的模块无需修改即可使用 RequireJS 加载。 There's one library I've authored which is distributed through npm and yet is a collection of AMD modules.我编写了一个库,它通过 npm 分发,但它是 AMD 模块的集合。 It is trivial to load them with RequireJS in the browser.在浏览器中使用 RequireJS 加载它们是微不足道的。

    B. It may require being wrapped in define calls. B. 它可能需要包含在define调用中。 I've recently loaded merge-options in one of my projects with gulp-wrap-amd .我最近使用gulp-wrap-amd在我的一个项目中加载了merge-options merge-options is a CommonJS module. merge-options是一个 CommonJS 模块。 It does not support RequireJS out-of-the-box but if you wrap it with a define call, it will work.它不支持 RequireJS 开箱即用,但如果您使用define调用包装它,它将起作用。

    C. It may require something more complex before it will be loaded in a browser. C. 它可能需要更复杂的东西才能加载到浏览器中。 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.例如,如果一个模块依赖于 Node 的fs模块,你就必须提供一个在浏览器中运行的fs的替代品。 It will presumably present a fake filesystem to your code.它可能会为您的代码提供一个虚假的文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM