Home > OS >  How to use old packages in NodeJS 18?
How to use old packages in NodeJS 18?

Time:11-23

Require function is not used anymore in NodeJS 18 (https://stackoverflow.com/a/63156878/610641).

But if you use the old package, how would you include it?

in my case, I'm trying to import package ip-range-check (https://www.npmjs.com/package/ip-range-check)

var ipRangeCheck = require("ip-range-check");

I get an error then: ReferenceError: require is not defined in ES module scope, you can use import instead

Then i tried to use import:

import * as ipRangeCheck from 'ip-range-check'
let isCorrectIP = ipRangeCheck(remoteIp, ".../28") || ipRangeCheck(remoteIp, "...0/24");

and then i get this error: TypeError: ipRangeCheck.ipRangeCheck is not a function

i printed out ipRangeCheck: [Module: null prototype] { default: [Function: check_many_cidrs] }

Not sure i understand well what it means. Does it mean the function check_many_cidrs is available as ipRangeCheck function?

I found some info that i have to change/remove type tag from package.json. Tried that, no luck.

And import looks well (according to documentation https://nodejs.dev/en/api/v18/esm/).

But definitely, I'm doing smth wrong here, or old modules do not work with new nodejs versions? I would appreciate any info/ideas.

CodePudding user response:

You can actually set it to commonjs instead of module in package.json

and just do the following and it will work

import ipRangeCheck from 'ip-range-check'
  • Related