It is because of the node-fetch package. As recent versions of this package only supports ESM; you have to downgrade it to an older version [email protected] or lower.
npm i [email protected]
This should solve the issue.
Answer from ABDULLOKH MUKHAMMADJONOV on Stack OverflowIt is because of the node-fetch package. As recent versions of this package only supports ESM; you have to downgrade it to an older version [email protected] or lower.
npm i [email protected]
This should solve the issue.
No need to use the old version. You can use this line instead of "require"
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
I've been in development of a library that is to be used as a framework for web applications. I've been using CommonJS for over a decade, but I felt like it was time to make the switch and rebuild my library using ESM format, BUT I need to be able to support both methods for implementation. After extensive reading and a bit of playing around, it seems like I will be unable to move to ESM and keep it backwards compatible with CommonJS without using a transpiler. That sucks. If anyone has any suggested ways of doing this, I'm all ears.
For context, I want to be able to import or require both named and default exports, such as:
// from an ESM module
import LibName, { Foo, Bar } from 'my-lib
// from a CJS file
const LibName = require('my-lib')
// or
const { Foo, Bar } = require('my-lib')I've started playing around with methods to get around it, but nothing seems to work. Creating multiple exports in my package.json sounded like a good way to go, but that basically requires the base code to be in CJS. Using ESM doesn't provide a way to create a CJS export "shim" because there's no way to dynamically import() and then export from a CJS file.
I know, a bunch of you will tell me to "forget about CJS, it's the past", but in order to make a library consumable by the masses, you need to be able to support both. I'm sure I'll also get advice about building it in Typescript, but I've tried it and I'm not a fan (I would love syntax for types built into JavaScript, but one of the great things about JS is that it doesn't need to be compiled and, well, TS fundamentally changes that).
I REALLY want to make the switch, but without full backwards compatibility to do everything the same from a client-code perspective, there just doesn't seem to be a way around it. I'm going to have to do it (again) using CommonJS formatting and then make it better compatible with ESM imports. Any advice?
I got the same error requiring node-fetch. The solution was
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
Use nodemon
- install nodemon if not installed
- create a nodemon.json file in root directory
- nodemon.json file should contain an object with a variable name to use for the environment. (i.e. {"env": { "enviornment_variable1":"somevalue", "environment_varialbe2":"somevalue" } } )
- Use the environment variable through process.env.environment_variable_key. In your something.js file you would make a variable for the environment variable (i.e. const x = process.env.enviornment_variable1
- Make sure to restart server
I'm trying to import node-stream-zip and electron-dl into a node application. It seems like I can have all libraries imported with either import or require, but I can't pick and choose. I'm toggling between them in package.json by setting
"type": "module"
If I set the type to module, I have to import everything with:
import {StreamZip} from "node-stream-zip"
import {download} from "electron-dl"
but this causes node-stream-zip to complain:
SyntaxError: Named export 'StreamZip' not found. The requested module 'node-stream-zip' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'node-stream-zip';
const {StreamZip} = pkg;Alternatively, I can import using:
const StreamZip = require('node-stream-zip');
const download = require("electron-dl")but this causes `electron-dl` to complain:
Error [ERR_REQUIRE_ESM]: require() of ES Module [mypath]\electron-dl\index.js from [mypath]\main.js not supported. Instead change the require of index.js in [my_path]\main.js to a dynamic import() which is available in all CommonJS modules.
It seems strange to me that I can only use one import syntax or the other in a single main.js. Is this the case? If so, is there any way to either get node-stream-zip or electron-dl to be okay with the other import syntax?