Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NodeJS script split into multiple files – require vs. import problems

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.".

>Solution :

So as you encountered on your journey, there are two module systems in NodeJS, ESM and CommonJS. I will now demonstrate the correct code for either system.

CommonJS

main.js can be left as-is:

const myCamelCaseFn = require('./lib/myLib');
myCamelCaseFn ("do something");

lib/myLib.js is currently (mostly) in ESM format. You’d need to change it to use CommonJS format:

const camelCase = require('camelcase');

const myCamelCaseFn = (arg) => { return camelCase(arg); }
exports.myCamelCaseFn = myCamelCaseFn;

However, this won’t work, not because of any error in syntax from our code, but because the dependency, camelcase is written solely for ESM format.

ESM

main.js is currently in CommonJS format. You’d need to change it to use ESM format. You also forgot the .js extension when you tried to change it to ESM format which was why it threw the "cannot find module" error:

import { myCamelCaseFn } from './lib/myLib.js';
myCamelCaseFn ("do something");

lib/myLib.js is currently mostly in ESM format but its exports is declared incorrectly. You’d do something like this instead:

import camelCase from 'camelcase';

export const myCamelCaseFn = (arg) => { return camelCase(arg); };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading