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

前端 未结 1 348
天命终不由人
天命终不由人 2021-01-02 15:16

I have a front-end SPA using RequireJS (2.1.14) as module system. It basically bootstrap and load Backbone.Marionette app.

In main

相关标签:
1条回答
  • 2021-01-02 15:30

    It is not possible to use RequireJS on the client (browser) to access files from 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. 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).

    To use your config module in the client app, you must first transform it into a RequireJS-compatible module and copy it under 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:

    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.

    0 讨论(0)
提交回复
热议问题