question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use node-libcurl with NextJS using native-ext-loader?

See original GitHub issue

I need Help please!

I am getting the following error when running my code on the browser:

Screen Shot 2021-02-20 at 9 20 19 AM

I am using NextJS on a Netlify build image that uses Ubuntu version 16.04 (aka Xenial) as the base.

my next.config.js is set as follows:

Screen Shot 2021-02-20 at 9 28 38 AM

As you can see I’m using native-ext-loader as a way to load your npm package into my build.

I’m assuming the error is basically saying it can not find the binary files that I am trying to include in the config above.

Please let me know if you need anything else in helping resolve this issue.

Thanks in advance, Antonio.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
9ntoniocommented, Mar 6, 2021

@JCMais I can confirm that your suggestion to use getServerSideProps addressed my issue. I had to use it as an async function per the following code. Please note I am using NextJS v10.0.6.

import { NextPage } from 'next';
import querystring from 'querystring';
const url = 'https://slack.com/';

interface Props {
  hostData?: string;
  slackCode?: any;
}

const Page: NextPage<Props> = ({ hostData, slackCode }) => (
  <main>

    <h1>Your data:&nbsp; 
      <u>{hostData}</u>
    </h1>

    <h1>Slack Data:<br /> 
      <u>{slackCode}</u>
    </h1>

  </main>
)

export async function getServerSideProps({ req, query }) {
  const{ Curl } = require('node-libcurl');
  const curl = new Curl();

  const hostData = req ? req.headers.host : 'NO HOST DATA';
  const slackCode = query.code;
  const data = {
    'code': slackCode,
    'client_id': process.env.CLIENT_ID,
    'client_secret': process.env.CLIENT_SECRET,
  }

  curl.setOpt(Curl.option.URL, url);
  // !! You need to build the query string,
  // !! node has this helper function for that:
  curl.setOpt(Curl.option.POSTFIELDS, querystring.stringify(data));
  curl.setOpt(Curl.option.VERBOSE, true);

  curl.on('end', (statusCode: any, body: any) => {
    console.log("\x1b[31m",`\n Body: \n${body}`);
    curl.close();
  })

  curl.on('error', curl.close.bind(curl));
  curl.perform();
  return { props: { hostData, slackCode } };
}

export default Page

For anyone trying this as a fix, please note to use the following structure of your returned data where data is whatever object you wish to return:

return { props: {data} }

If you do not use this structure you will get an error.

Thank you for your help, and I hope this helps anyone who has the same issue I did.

0reactions
JCMaiscommented, Mar 6, 2021

Glad you got it working @9ntonio, and thanks for posting the full solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Repercussions of node-pre-gyp's find method · Issue #204 · JCMais ...
The usage of a dynamic require(..) call for locating the binding. This require statement to load the native binding: node-libcurl/lib/ ...
Read more >
Upgrade Guide - Next.js
To update to Next.js version 13, run the following command using your preferred ... and style images, better accessibility, and native browser lazy...
Read more >
Making cURL Requests in Node.js - Section.io
cURL (client URL) is a free tool used to make network requests from the terminal using various protocols available.
Read more >
curl.js AMD Loader - David Walsh Blog
This configuration allows you to provide plugin paths, modules paths, and more. Basic define and require with curl.js. Basic usage of curl.js is ......
Read more >
node-libcurl installation fails on MacOS Catalina: clang: error
The problem comes from newer versions of Xcode command line tools on recent MacOS versions. The crucial part in the log is:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found