Node.js - Must use import to load ES Module: [duplicate] - node.js

This question already has answers here:
Importing in Node.js: error "Must use import to load ES Module" [duplicate]
(7 answers)
Closed 1 year ago.
I googled for a while but I could not find a solution that fixed my issue.
I want to use the fetch function from "node-fetch", but when requiring it in my code, I get the error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/xvma/stuff/Projekte/React/first-try/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /Users/xvma/stuff/Projekte/React/first-try/node_modules/node-fetch/src/index.js from /Users/xvma/stuff/Projekte/React/first-try/test.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/xvma/stuff/Projekte/React/first-try/node_modules/node-fetch/package.json.
Terminal output
In this file I want to use fetch()
My server.js
I hope you guys can help me, I tried deleting my node-components folder & package lock but that did not work.
Also, in my package.lock.json "type"="module" is not set.

It looks like you're using node-fetch 3, which is only distributed as an ESM module, not require()able CJS modules.
For the time being, you can just downgrade to a node-fetch less than version 3.
Alternately, you can go all in on ESM and rename your script to server.mjs and use import everywhere.

Related

Typescript Support for JS Library (node-gtf) in a Express Project

Have a Typescript Express Server for consuming GTFS data using the GTFS library (https://github.com/BlinkTagInc/node-gtfs)
version ("gtfs": "^3.0.4")
Importing the library this way
import { importGtfs } from 'gtfs';
But due to no TS support I m facing this error
require() of ES modules is not supported.
require() of <Project-path>/node_modules/gtfs/index.js from <Project-path>/src/index.ts is an ES module file as it is a .js file whose nearest parent package.
json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from <Project-path>/node_modules/gtfs/package.json.
Using it this way
import { importGtfs } from 'gtfs';
Found a type Library for GTFS
https://www.npmjs.com/package/gtfs-types
But unable to find a solution to include these types in the Project
This issue is caused by the "type": "module" property in package.json of the gtfs version you're using
type = module would expect es6 syntax however somewhere commonjs require is used.
this is a known issue with node v12.11.0 which was resolved in v12.11.1
you could downgrade the gtfs version to 2.4.4 which doesn't have this type:module property.
If this is a dependency of dependency then add it to resolutions
"resolutions":{
"gtfs": "^2.4.4"
}

How to use named imports (ES 6) in NodeJS 14.x?

I have code which I like to use inside nodejs (14.4) and the browser. For this code to work inside nodejs I need named imports like
import {Vector3} from "three;
ES 6 modules in general are working fine with the following changes:
package.json:
"type": "module",
An launching nodejs with --experimental-specifier-resolution=node so I don't have to specify file extensions. But for named imports nodejs still prints out:
SyntaxError: The requested module 'three' is expected to be of type CommonJS, which does not support named exports.
There is a Stackoverflow post suggesting the usage of esm package loader. Unfortunately it has a bug making TypeScript "reflect-metadata" unusable (Issue: https://github.com/standard-things/esm/issues/809) So I can't use that.
TL;DR; How can I enable named ES 6 modules in nodeJs 14.4 without ESM package loader? type: module and launch arg are already set.
This may not be the perfect answer but I solved my problem by switching the code base to Type Script (TS):
For browser, I can configure ts to use es 6 modules
For node, I can configure ts to use node modules
--> Everybody is happy

How can I fix error with react app creation? [duplicate]

This question already has answers here:
npx create-react-app not working "Must use import to load ES Module:" [closed]
(4 answers)
Closed 2 years ago.
After run comand npx create-react-app projectName i've recieved this log, and app not created. Why this error happens?
npx: installed 99 in 8.22s
Must use import to load ES Module: C:\Users\Roman\AppData\Roaming\npm-cache_npx\12568\node_modules\create-react-app\node_modules\is-promise\index.js
require() of ES modules is not supported.
require() of C:\Users\Roman\AppData\Roaming\npm-cache_npx\12568\node_modules\create-react-app\node_modules\is-promise\index.js from C:\Users\Roman\AppData\Roaming\npm-cache_npx\12568\node_modules\create-react-app\node_modules\run-async\index.js is an ES module file
as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\Roman\AppData\Roaming\npm-cache_npx\12568\node_modules\create-react-app\node_modules\is-promise\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\Roman\AppData\Roaming\npm-cache_npx\1
2568\node_modules\create-react-app\node_modules\is-promise\package.json.
This error is related to the changes that was introduced in: is-promise package version 2.2.0 (that is in node_modules of create-react-app) in which was added support for ES Module style default import, but in version 2.2.2, ESM support was removed because it turns out to be a breaking change.
Try this:
npm install -g --force create-react-app
to update the packages.

Use an es module package in a commonjs project?

I'm trying to use an es module package with a commonjs project. I'm getting the error
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/user/Documents/git/test/node_modules/logger/lib/index.js
require() of ES modules is not supported.
require() of /Users/user/Documents/git/test/node_modules/logger/lib/index.js from /Users/user/Documents/git/test/testLogger.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/user/Documents/git/test/node_modules/logger/package.json.
The package in question is actually my own package. What can I do to make it work with commonjs (other than replacing all the imports)?
You need install babel-register and babel-preset-env
// add to index.js or start.js
require('babel-register')({
presets: ['env']
});
See more here https://appdividend.com/2019/01/23/javascript-import-statement-tutorial-with-example/

Can I use ES6 modules (import export) to write webpack config files?

I started seeing:
(node:6420) Warning: require() of ES modules is not supported.
when starting my webpack build and was wondering if using all import export for webpack.config.js was supported yet.
edit 1: I want to know if it's supported without using #babel/register or other transforms
Is is supported in Node 13. You can use either the .mjs extension (for files where you need to use import/export), or set "type": "module" in your package.json.
If your code runs in Node, you can use the fs package to interact with the file system

Resources