Search code examples
javascriptnode.jsapitumblr

Problems concerning require() javascript


I'm trying to run the JavaScript client for the Tumblr API but I'm getting a "require not defined" error. I've scoured almost every corner of SO trying to find a way to make it work.

I've managed to use Browserify to recognize it but at some point, I'm getting a fs.readFileSync error.

Then I tried RequireJS and again it does the require thing but I'm getting a Module name "something" has not been loaded yet for context"

I'm really new to Node.js but if I'm understanding it correctly the require() part isn't something the browser can do right? Can anyone guide me as to what solutions I can take for this? If it helps, I'm not planning to upload this on a server or anything. I'm just trying to run it on my local computer and just planning to run the API locally.


Edit: Here's what I have on my main.js

var tumblr = require('index.js');

var client = tumblr.createClient({
  consumer_key: '<consumer key>',
  consumer_secret: '<consumer secret>',
  token: '<token>',
  token_secret: '<token secret>'
});

// Show user's blog names
client.userInfo(function (err, data) {
  data.user.blogs.forEach(function (blog) {
    alert('dang');
   });
});

When I try requirejs, I do this to reference it <script data-main="script/main" src="scripts/require.js"></script>


Solution

  • It seems to me that you might be confusing server-side JavaScript (Node) and client-side JavaScript (in the browser). The Tumblr API you're trying to use is for Node only; you can't run it in a browser. You have to:

    1. Install NodeJS and NPM on your system
    2. npm install tumblr.js
    3. Run the script with node <script name>

    Start with the Tumblr.js example script, then customize for your own purposes!