![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
NRAW.js simplifies the use of Reddits API with easy-to-use chainable functions.
NRAW.js simplifies the use of Reddit's API with an easy-to-use chainable functions.
Start by installing NRAW.js:
$ npm install nraw
Then require it in your server:
var r = require("nraw");
Now, make a new instance of the r
-object, which you can do by providing a User-agent.
var Reddit = new r("Testbot v0.0.1 by Mobilpadde");
Or you can fill in three parameters, which are a user-agent, a cookie and the modhash of a Reddit user:
var Reddit = new r("Testbot v0.0.1 by Mobilpadde", cookie, modhash);
Executing requests can be done in two ways:
Reddit.user("Mobilpadde").exec(function(data){
// Some super awesome code
})
Or
Reddit.user("Mobilpadde", function(data){
// Some super awesome code
})
In the examples below, the first method will be applied.
Get the 25 latest posts (Links, self-posts or comments) of a given user:
Reddit.user("Mobilpadde").exec(function(data){
// Some super awesome code
})
"That's stupid! Who'd ever need the latest 25 posts of a user?", you say? Well, NRAW.js is smart enough to handle queries too!
Let's make a basic request that gets the top five posts (Links, self-posts and comments) of a user:
Reddit.user("Mobilpadde").sort("top").limit(5).exec(function(data){
// Some super awesome code
})
Pretty cool, 'eh?
"But what if I want to see seven of my liked posts and comments?" - That's super easy too! Simply use the login
-function and the liked
-filter:
Reddit.login(user, pass).user("Mobilpadde").liked().limit(7).exec(function(data){
// Some super awesome code
})
Get the 25 latest posts (Links and self-posts) of a given subreddit:
Reddit.subreddit("CatReactionGifs").exec(function(data){
// Some super awesome code
})
"That's not useful at all! I hate you" - You.
Well, like last time, we have queries that can be applied, but wait! There's more! Subreddits even have the ability to be filtered!
Let's make a request of the 25 most controversial posts from the last year from a given subreddit, but only the ones after the post with the id t3_2k0r3o
:
Reddit.subreddit("CatReactionGifs").controversial().after("t3_2k0r3o").exec(function(data){
// Some super awesome code
})
Or how about we make a request, that finds the 42 top comments of a given subreddit, using the filter comments
:
Reddit.subreddit("CatReactionGifs").comments().top().limit(42).exec(function(data){
// Some super awesome code
})
"Can I get a single post if I know its id?" - Easy peasy!
Reddit.subreddit("CatReactionGifs").post("2zmdf9").exec(function(data){
// Some super awesome code
})
"Whoa, that's pretty awesome, but can I post a link?" - Of course you can! We just need to login and use the post
-function:
Reddit.login(user, pass).subreddit("CatReactionGifs").post().link("How I feel when there's only one pizza slice left", "http://i.imgur.com/CFSwHdq.gif").exec(function(data){
// Some super awesome code
})
"Wow! C-c-can I subscribe to subreddits then?" - Yea you can! Though we'll have to get our hands a bit dirty:
Reddit.subreddit("CatReactionGifs", function(info){
Reddit.login(user, pass).subreddit(info.data.children[0].data.subreddit_id).subscribe(function(data){
// Some super awesome code
})
})
"Awesome! But what if I don't like a subreddit anymore?" - Well, that's a bit tougher! Ha! Gotcha! You should've seen your face! Priceless! Don't worry, it's super easy too:
Reddit.subreddit("DogReactionGifs", function(info){
Reddit.login(user, pass).subreddit(info.data.children[0].data.subreddit_id).unsubscribe(function(data){
// Some super awesome code
})
})
We can even search through a subreddit:
Reddit.subreddit("CatReactionGifs").search("Cat").exec(function(data){
// Some super awesome code
})
"What if I want a random thread from a given subreddit?" - Pure easiness! Simply do:
Reddit.subreddit("cats").random().exec(function(data){
// Some super awesome code
})
You can also get multireddits:
Reddit.user("Mobilpadde").multireddit("kittehs").exec(function(data){
// Some super awesome code
})
How about we play around with some comments for a while?
Let's get all the new comments (Login so we don't have to wait 30 seconds before we can request new comments):
Reddit.login(user, pass).comments().exec(function(data){
// Some super awesome code
})
"Well, now for a tough one! Can I post comments?" - Yes! Yes you can! All you need is an id of the parent (In this case we're gonna use t3_31cvo9
):
Reddit.login(user, pass).post().comment("t3_31cvo9", "I love you!").exec(function(data){
// Some super awesome code
})
"What if I want to see a specific comment and I have its id?" - Well, that's super easy like everything else:
Reddit.subreddit("CatReactionGifs").post("2zmdf9").comment("cpkmvc4").exec(function(data){
// Some super awesome code
})
"I want to delete my comment, please help!" - Alrighty! It's as easy as pie:
Reddit.login(user, pass).comment("t1_cq0ev3j").delete().exec(function(data){
// Some super awesome code
})
You can also delete a post if you misspelled something:
Reddit.login(user, pass).post("t3_31cvo9").delete().exec(function(data){
// Some super awesome code
})
"But I want the related posts of a post to which I know the id" - That's even easier! Take a look at this:
Reddit.post("2v5oi5").related().exec(function(data){
// Some super awesome code
})
Upvoting:
Reddit.login(user, pass).post("t3_2v5oi5").upvote().exec(function(data){
// Some super awesome code
})
Unvoting:
Reddit.login(user, pass).post("t3_2v5oi5").unvote().exec(function(data){
// Some super awesome code
})
Downvoting:
Reddit.login(user, pass).post("t3_2v5oi5").downvote().exec(function(data){
// Some super awesome code
})
Ps. add .comment("t1_coestfz")
, if you want to vote on a comment instead of a post.
You can also search for every thread - in every subreddit - containing the word cat
:
Reddit.search("cat").exec(function(data){
// Some super awesome code
})
How 'bout getting a random thread, you say?
Reddit.random().exec(function(data){
// Some super awesome code
})
FAQs
NRAW.js simplifies the use of Reddits API with easy-to-use chainable functions.
The npm package nraw receives a total of 7 weekly downloads. As such, nraw popularity was classified as not popular.
We found that nraw demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.