I have 2 files:
|- main.js
|- lib
|- myLib.js
I added a Node.js package (camelcase in this case) to the project using NPM:
"dependencies": {
"camelcase": "^7.0.1"
}
In myLib.js I need that the camelCase function coming from this package. The myLib script defines some utility functions used in main.js.
First, I tried to build the 2 files without including the “type”:”module” to package.json:
// main.js
const myCamelCaseFn = require('./lib/myLib');
myCamelCaseFn ("do something");
// myLib.js
import camelCase from 'camelcase';
const myCamelCaseFn = (arg) => { return camelCase(arg); }
exports.myCamelCaseFn = myCamelCaseFn;
I got
cannot use import statement outside a module
I read some pages, then I found out that this form of import only usable when “type”:”module” is included to package.json. So I did it.
Then there were problems with the require() call, so I changed to the following:
// main.js
import {myCamelCaseFn} from './lib/myLib';
myCamelCaseFn ("do something");
Now I got
cannot find module lib/myLib
So I changed to
const myCamelCaseFn = import('./utils/renamer.js');
console.log(myCamelCaseFn);
myCamelCaseFn ("do something");
I got
myCamelCaseFn is not function
and I see the console message this is a Promise { pending }
. As I read import function returns with a promise which will be resolved later – but I can wait to be resolved here (not needed using Require()
as it works in another way). I changed again:
const myCamelCaseFn = await import('./utils/renamer.js');
myCamelCaseFn ("do something");
Now I got a different error message:
ReferenceError: exports is not defined in ES module scope
exports.myCamelCaseFn = myCamelCaseFn;
Continue reading it says exports and module.exports points to the same object. Later I got the following on Stack Overflow “Try and remove “type”: “module” from package.json.”.