# Facebook SDK for JavaScript with RequireJS In this tutorial, you'll learn how to incorporate the [Facebook SDK for JavaScript](https://developers.facebook.com/documentation/javascript/quickstart) with other JavaScript modules using RequireJS. Ordinarily, the JavaScript SDK isn’t compatible with the [Asynchronous Module Definition](http://requirejs.org/docs/whyamd.html) (AMD) design pattern, so this tutorial covers writing a shim to provide the FB object created by the SDK. This tutorial assumes you’re familiar with RequireJS and JavaScript modules. [Find out more about RequireJS](http://requirejs.org). ## Configuration {#configuration} Configure your other RequireJS scripts as normal, and add a new .js file for interaction with the Facebook SDK. This project assumes a directory structure like the one below: ``` - project/ - index.html - scripts/ - main.js - require.js ``` Add a new file for configuring and interacting with the SDK, as below: ``` - project/ - index.html - scripts/ - main.js - require.js - fb.js ``` You should be importing the `requirejs` script and declaring `main.js` as your data-main as follows: ``` <script data-main="scripts/main" src="scripts/require.js"></script> ``` ## Adding a shim to the Facebook SDK In your main project script, add a shim declaration to the `require.config`, as shown: ``` require.config({ shim: { 'facebook' : { exports: 'FB' } }, paths: { 'facebook': 'https://connect.facebook.net/en_US/sdk.js' } }) require(['fb']); ``` This creates a `facebook` module, using the JavaScript SDK URL, and marks the `FB` object as the export for that module. In your newly-created `fb.js`, you can instantiate and use the FB object as usual. Add the App ID for your app from the [App Dashboard](https://developers.facebook.com/apps). You just need to wrap your code in a define block, passing the `facebook` shim module as a required dependency. ``` define(['facebook'], function(){ FB.init({ appId : '{your-app-id}', version : 'v25.0' }); FB.getLoginStatus(function(response) { console.log(response); }); }); ``` ## See Also {#seealso} * [Getting Started with the Facebook SDK for JavaScript](https://developers.facebook.com/documentation/javascript/quickstart) * [JavaScript SDK Reference](https://developers.facebook.com/documentation/javascript)
9188d5f
Created abwafndynaalsal@gmail.com (markdown)
230ea70
Created _Footer (markdown)
9f28813
Created Damarcus Jones Ai Chips (markdown)
5833e96
Created _Sidebar (markdown)
84c89d3
Created AMD Forks (markdown)
30e512d
Created SW:m1n1-Damarcusjonesaichips (markdown)
7b5bdad
Destroyed Header (markdown)
6fabfaa
Destroyed RichShops (mediawiki)
6385259
Revert da349862d85438c29a41d2c99e703bcc9762dbcb...6255c029689515b3d246726ac82e31486d084507 on AMD
54b0b5b
Created Header (markdown)
96ac79c
Destroyed AMD Forks (markdown)
8c73bb6
Destroyed Common Config (markdown)
fc3188c
Destroyed _Footer (markdown)
8b47759
Updated _Footer (markdown)
31542da
Destroyed AMD (中文版 (markdown)
439db76
พัฒนา การรักษาความปลอดภัยให้ชุมชน
cb361ac
Created AMD (中文版 (markdown)
9758eea
Updated _Footer (markdown)
0ec4e18
Destroyed _Sidebar (markdown)
3283f12
Destroyed _Sidebar (markdown)
ac905b2
Created _Sidebar (markdown)
a7dee0b
Destroyed AMD (中文版 (markdown)
8a933c1
Created AMD (中文版 (markdown)
7d5b607
Updated _Footer (markdown)
124780f