Update: more details to explain the problem clearly
I created an ES module (that works fine) and looks like so
//********** package.json ********
…
"type": "module",
…
//********************************
//********** index.js ************
import * as fs from 'fs';
…
class Foo { … }
…
export { Foo };
//********************************
//********** test.js *************
import { Foo } from './index.js';
…
//********************************
I want to convert the above so it can be used either of the following ways
// ES module
import { Foo } from 'myModule';
// CommonJS module
const Foo = require('myModule');
I can't figure out how to do that?
Update2: based on a similar question, I changed the code like so
//********** package.json ********
…
"type": "module",
…
//********************************
//********** index.js ************
import * as fs from 'fs';
…
class Foo { … }
…
// export { Foo };
module.exports = Foo;
//********************************
//********** test.js *************
// import { Foo } from './index.js';
import * as Foo from '../index.js';
…
//********************************
Now I get the following error
file:///Users/punkish/Projects/foo/index.js:138
module.exports = Foo;
^
ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/punkish/Projects/foo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.