简体   繁体   English

如何在RequireJS(AMD)环境中访问node.js模块?

[英]How to access node.js module in RequireJS (AMD) environment?

I have a front-end SPA using RequireJS (2.1.14) as module system. 我有一个使用RequireJS (2.1.14)作为模块系统的前端SPA。 It basically bootstrap and load Backbone.Marionette app. 它基本上是bootstrap并加载Backbone.Marionette应用程序。

In main.js : main.js

require.config ({
  baseUrl: '/js',
  waitSeconds: 200,
  nodeRequire: require,
  paths: {
    jquery: '//cdn/jquery.min',        
    underscore:'//cdn/underscore-min',
    // more plugins
  },
  shim: {
      // shimming stuff
  }
});

require(['marionette',
    'vent',
    'config/template',
    'app',
    'routers/main'
   ],
function (Marionette,
      vent,
      Template,
      nrtApp
) {
'use strict';

nrtApp.module ('Public.Main', function (Main, nrtApp, Backbone,Marionette, $, _) {
  nrtApp.start ();

  // this is where the error is:
  requirejs (['config'], function (config) {
    if (typeof config !== 'undefined') {config.log ('ok!');}
  });

});

});

The issue is, I would like load some npm packages (eg npm install config) from RequireJS module. 问题是,我想从RequireJS模块加载一些npm包(例如npm install config)。 RequireJS can't seem to find npm node_modules directory which is sitting at different directory than RequireJS baseUrl directory . RequireJS似乎无法找到位于与RequireJS baseUrl node_modules目录不同的目录的npm node_modules目录

Below is my directory structure: 以下是我的目录结构:

my_project/
    app/
        public/
            js/
                main.js
                app.js
    node_modules/
        config/

Below is error message: 以下是错误消息:

脚本错误

It tried to load module from baseUrl directory. 它试图从baseUrl目录加载模块。

How can I access npm module to from RequireJS module system in my use case? 如何在我的用例中从RequireJS模块系统访问npm模块?

It is not possible to use RequireJS on the client (browser) to access files from node_modules. 无法在客户端(浏览器)上使用RequireJS来访问node_modules.文件node_modules. Files under node_modules must first be copied to a location that is accessible (under the public folder) before the client can access them. 下的文件node_modules必须首先被复制到可访问(下一个位置public文件夹)之前在客户端可以访问它们。 The documentation says RequireJS can access Node modules , but this only works for server-side JavaScript (when you want to use RequireJS-style module syntax in Node). 文档说RequireJS可以访问Node模块 ,但这仅适用于服务器端 JavaScript(当您想在Node中使用RequireJS样式的模块语法时)。

To use your config module in the client app, you must first transform it into a RequireJS-compatible module and copy it under public . 要在客户端应用程序中使用config模块,必须先将其转换为与RequireJS兼容的模块,然后将其复制到public模块下。 This article explains how to automate this with package managers and build tools , and contains the quote that finally fixed my broken understanding of RequireJS + Node: 本文解释了如何使用包管理器和构建工具自动执行此操作 ,并包含最终修复了我对RequireJS + Node的理解的引用:

In order to use node modules on the browser you will need a build tool. 要在浏览器上使用节点模块,您需要一个构建工具。 This might bother you. 这可能会打扰你。 If this is a deal breaker then by all means continue wasting your life copying and pasting code and arranging your script tags. 如果这是一个交易破坏者,那么无论如何都要继续浪费你的生命来复制和粘贴代码并安排你的脚本标签。

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

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