jkhsjdhjs / node-fetch-cookies

node-fetch wrapper that adds support for cookie-jars
MIT License
28 stars 16 forks source link

Cannot import module #3

Closed tcaer closed 4 years ago

tcaer commented 4 years ago

I am trying to import the module like so: const { CookieJar, fetch } = require('node-fetch-cookies'); but I am getting this error message:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/tino/projects/bot/node_modules/node-fetch-cookies/src/index.mjs
    at Module.load (internal/modules/cjs/loader.js:1000:11)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/Users/tino/projects/bot/src/supreme/product-service.js:2:23)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19) {
  code: 'ERR_REQUIRE_ESM'
}
jkhsjdhjs commented 4 years ago

This package is an ES module, not CJS. You have to import it like this:

import { CookieJar, fetch } from 'node-fetch-cookies';

If you can't use ES modules for some reason or if you don't want to use them, you can also load it using the esm package:

const esmImport = require('esm')(module);
const { CookieJar, fetch } = esmImport('node-fetch-cookies');
pinturic commented 4 years ago

To me this made the trick: https://github.com/lukeed/webpack-modules

The module is imported correctly by using this importer

Flummi commented 4 years ago

?

CyanoFresh commented 3 years ago

Why did you choose esm modules? It's not even in LTS node, now I need to download more packages just to run this one

jkhsjdhjs commented 3 years ago

I've chosen ESM because of the cleaner import/export syntax. ESM modules are in node LTS behind the --experimental-modules feature flag and this library is tested on node LTS. In three months node v16 will be LTS, which includes ESM support without a feature flag. Sure, I started using ESM really early, but at this point every users is free in their choice between ESM or CJS. If it's possible to add CJS support without copying the whole project and adjusting the imports/exports (I haven't looked into that yet), feel free to open a PR. If not and you choose to use CJS you'll have to live with using esmImport.