NodeJS Work with Type module and Require

84 / 100

Dabbling with NodeJS again and this time bump into a bumpy road trying to make require to work with type module.

Basically trying to add the line below

"type" : "module"

into package.json to utilize the import function of JavaScript by including the above type.

  "name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "excel4node": "^1.7.2",
    "express": "^4.17.1",
    "mssql": "^6.3.0"

After looking through few JavaScript tutorials (after dabbling a lot on Java, decided it’s time to move on to other skill sets), I find writing in NodeJS easier.

Type Module Is Not Working

I have summarize the following after trying some things.

NodeJS Type Module Javascript

Do Not Use Import… from

Tried to do something like this for my JavaScript

import {firstSQLFunction} from './Promise1.js';
promise2 = firstSQLFunction(worksheet2,style);


export function firstSQLFunction(worksheet2, style) {
    var returnPromise1 = new Promise(function(resolve, reject) {
    // bunch of codes
            if(true) {
                resolve('Completed SQL 2');
            } else {
                reject('connection2 refused');

    return returnPromise1;

The above should work, but there is a problem. You would not be able to use require in your NodeJS project after that.

var express = require('express');
var app = express();

The below error message would appear if you tried doing so.

ReferenceError: require is not defined


If you are using require, please refrain from adding “type”: “module” into your package.json as this will interfere with each other. If you really and really need to use “type”: “module”, then you should use import from instead of require.

This is because it is either/or usage of these two. That is because one is a ESM and one is

ESM (EcmaScript modules) is a newer system which has been added to the JavaScript specification. Browsers already support ES modules, and Node is adding support


CJS is short for CommonJS. Here is what it looks like: //importing const doSomething = require(‘./doSomething.js‘); //exporting module. exports = function doSomething(n) { // do something } Some of you may immediately recognize CJS syntax from node


So What Did I Do?

technical person sleep
Ahaha!!! I Wished

I decided that I should just move on and go with the new syntax of using the CJS way of doing it. So that meant I would not be using anymore Type Module in my JavaScript.

module.exports = {

    firstSQLFunction: (worksheet2, style) => {
        var returnPromise1 = new Promise((resolve, reject) => {
                if(true) {
                resolve('Completed SQL 2');
            } else {
                reject('connection2 refused');

        return returnPromise1;

The above script is now clean and we can now focus on using just CJS styled JavaScript. What do you know, it’s good to pick up more skills and prove you’re still in the know. This will ensure you are still relevant in the modern technology world.

I will go start moving into YouTube as well in the future for more exposure. Till then I will continue to write my findings and journey in this technology blog.

Stay tune.

Leave a Reply