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/
Related
I have got the latest version of npm modules and developing a nodejs application. The problem is that some npm modules support the require() and the others support the import/export statements, I cannot use them both in a file.
Having the production criteria in mind, which should I opt for either only require() or only import/export or a mix of those using the dynamic import() along with the require(). Thanks
I assume we are talking about plain JavaScript without transpiling from TypeScript or Babel.
The require() function is used to load CommonJS modules whereas the import statement is used to load ESM modules. Which of them you should or can use depends on factors such as:
does your target Node.js version support ESM? I think ESM in Nodejs support got introduced in v12, but I'm not sure how stable it is.
is your package defined as ESM or CommonJS module. That depends on the type field in your package.json, with "type": "module" for ESM and "type": "commonjs" for CommonJS: https://nodejs.org/api/packages.html#type
are your package dependencies provided as ESM or CommonJS modules. That depends on each individual package.json and their type field as well as if the package is provided as both ESM and CommonJS modules with conditional exports: https://nodejs.org/api/packages.html#conditional-exports
Unfortunately, I cannot provide a definite answer for you because that depends on your environment. ESM and import statements are going to be the future of JS, but the ESM support in the Node.js ecosystem is not on par with CommonJS yet.
With this in mind, I would opt for CommonJS modules but I would use a transpiler like TypeScript so I can still use modern features of JS.
I'm working with electron.js, and I need to use an ES Module I made for an other project.
How can I include this module in my project knowing that electron js doesn't works with ES Module, and it doesn't allow me to set :
{
"type": "module"
}
in the package.json
thx
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.
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"
}
Ecmascript Modules are the future for packaging JS code, and both Node.js and Coffeescript support them. But I've had some trouble getting their support for ESM to work together.
The current stable Node (12.x) has ESM modules behind a flag (--experimental-modules). Coffeescript supports passing flags through to Node with --nodejs. So with a couple of files using ESM modules:
# a.coffee
import b from './b.coffee'
b()
# b.coffee
b = ->
console.log "Hello"
export default b
In theory, we can run this code with npx coffee --nodejs --experimental-modules a.coffee. In practice this raises an error:
13:24 $ npx coffee --nodejs --experimental-modules a.coffee
(node:8923) ExperimentalWarning: The ESM module loader is experimental.
(node:8923) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/media/projects/coffeemodules/a.coffee:1
import b from './b.coffee';
^^^^^^
SyntaxError: Cannot use import statement outside a module
...
The error and docs say there are two ways to flag a file as containing an ESM module, one is to use the mjs extension (which isn't available to us here), and the other is to set "type": "module" in package.json, which also doesn't seem to work.
So: can it be done? Is there a way to get Coffeescript and Node.js ES modules to play together?