How to use ES Modules in Electron.js project - node.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

Related

How can I run an ExpressJS API using module instead of commonjs and has been bundled by Webpack?

In my express app I use Import and Export statements. I have the "type": "module" line in my package.json file. I can run the app using Node before bundling it with Webpack.
After I've bundled it into one JS file, Webpack converts the Imports to "require". Then I get the "ReferenceError: require is not defined" error when I try to run the bundled JS file using Node.I would like to use modules instead of converting my project to commonjs.
Webpack v.5
Node v.14
Express v.4

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

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