skip to Main Content

I want to use this module in my nodejs app.

the issue is I used a lot of modules with require statement before, like:

const translate = require('google-translate-open-api');

Now when i want to use the mentioned module using this:

import { HttpProxyAgent } from 'http-proxy-agent';

I get this error:

SyntaxError: Cannot use import statement outside a module

if I use this code to import the module:

const { HttpProxyAgent }= require('http-proxy-agent');

I get this error:

TypeError: createHttpProxyAgent is not a function

How can I use this kind of modules in nodejs app?

2

Answers


  1. Simply wrap HttpProxyAgent inside {} just like the import one.

    const {HttpProxyAgent} = require('http-proxy-agent');
    
    
    Login or Signup to reply.
  2. in your package.json file add:

    {
     // ...
     "type": "module",
      // ...
    }
    

    and then use:

    import { HttpProxyAgent } from 'http-proxy-agent';
    

    you might also need to make changes in other imports which use require keyword.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search