RequireJS support #303

Closed
charandas opened this Issue May 13, 2014 · 7 comments

Projects

None yet

8 participants

@charandas

Does atom shell plan to support AMD/RequireJS code in browser scripts?

I tried to run my AMD-based project and I am getting following:

$ ../atom-shell-v0.12.4-win32/atom.exe atomApp
[12016:0513/123139:INFO:renderer_main.cc(227)] Renderer process started
[11164:0513/123139:INFO:CONSOLE(3)] "Uncaught TypeError: Object function require
(path) {
    return self.require(path);
  } has no method 'config'", source: file:///C:/Users/karan_001/code/atomApp/app
/js/config.js (3)

On line 3 of config.js where it erred out, the JavaScript has a call to require.config. Is it conflicting with the CommonJS require definition. I found r.js but looks like its for running node-only projects that are written in AMD, not browser code written in AMD.

Feel free to enlighten me on the subject. Thanks.

@zcbenz zcbenz added the wontfix label May 14, 2014
@zcbenz
Contributor
zcbenz commented May 14, 2014

No there is no plan for supporting it in atom-shell, we are using the exact same module system with node, adding our own extensions would cause confusions.

@zcbenz zcbenz closed this May 14, 2014
@jhiggins-thrillist
Contributor

I think it is possible to use window.requirejs

@ssbb
ssbb commented May 17, 2014

Works for me with r.js. In this case, the variable names are optimized (shortened) and there is no conflict with the require.

@gmr
Contributor
gmr commented May 20, 2014

Just as an aside, I have requiresjs working well by re-assigning require to nodereq before my requirejs config:

nodereq = require
requirejs.config({})

When I need something like ipc or what not, I just use ipc = nodereq('ipc')

@julmot
julmot commented Apr 18, 2016 edited

I have the same issue using require.config. @jhiggins-thrillist using window.require doesn't solve this for me.

@art-ist
art-ist commented Apr 24, 2016 edited

The Electron Faq documents that case perfectly:

// In the main process.
var mainWindow = new BrowserWindow({
  webPreferences: { nodeIntegration: false }
});

I could take my browser targeting rjs-output as is and my bundled AMD modules as well as excluded local modules now load in electron.
Just

  • dealing with CORS to call the service api,
  • and addng electron specific functionality like filesystem access

had to be done now.

For the latter, adding the following to the beginning of a electron specific index.html. To keep the node-specific require. As this version is (in the browser-tread) only used for electron-specific functionality it seems to be a much better idea to rename this one than digg through all references of the browser-require and rename those.

<script>
window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;
</script>

I had plenty of troubles getting my require build process to run but electron integration turned out to be super easy and perfectly suported by the docs.
@zcbenz I disagree: it works perfectly (no further plans needed ;). Great cheers to the electron team!

@tiemevanveen

Despite your tips, I'm still getting this error while trying to load RequireJS into my webview which is loaded in a BrowserWindow.

require.dev.js:168 Uncaught Error: Mismatched anonymous define() module: function () {

Did somebody make an example repo by any chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment