Engineer bro!

Sat May 07 2022 (2 weeks ago)

Engineer by mistake!

I am a software engineer by passion

Using import/export in NodeJs

The export statement is used when creating JavaScript modules to export live bindings to functions, objects, or primitive values from the module so they can be used by other programs with the import statement. The value of an imported binding is subject to change in the module that exports it.

The aim of this article is to explain you that how can you use import/export statement in your project. This article won’t provide you the import/export explanation. For explanation you can refer here

Without import/export

Let's create three files:

  • sum.js

  • sub.js

  • index.js

sum.js

function sum(a, b) {
  return a + b;
}

module.exports = sum;

sub.js

function sub(a, b) {
  return a - b;
}

module.exports = sub;

index.js

const sum = require("./sum");
const sub = require("./sub");

console.log("Sum of 5 & 3 is", sum(5, 3));
console.log("Sub of 5 & 3 is", sub(5, 3));

When we'll run index.js then we'll get the output.

Sum of 5 & 3 is 8
Sub of 5 & 3 is 2

With import/export

sum.js

function sum(a, b) {
  return a + b;
}

export default sum;

sub.js

function sub(a, b) {
  return a - b;
}

export default sub;

index.js

import sum from "./sum";
import sub from "./sub";

console.log("Sum of 5 & 3 is", sum(5, 3));
console.log("Sub of 5 & 3 is", sub(5, 3));

When you'll run the index.js then you’ll get the following error.

(node:5177) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/rahul/Desktop/projects/test/index.js:1
import sum from "./sum";
^^^^^^

SyntaxError: Cannot use import statement outside a module

Solving - Cannot use import statement outside a module

If you'll add "type":"module" in your package.json then you will be able to execute the index.js

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type":"module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Let's create another file named log.js with the below content:

export default function () {
  console.log("Logged");
}

Now, let's modify sum.js

import log from "./log";

function sum(a, b) {
  log();
  return a + b;
}

export default sum;

Now, if you'll execute then you'll get the following error.

node:internal/process/esm_loader:91
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/rahul/Desktop/projects/test/log' imported from /Users/rahul/Desktop/projects/test/sum.js
Did you mean to import ../log.js?

Solving - Cannot find module

If you just want to get rid of the issue and execute the file then you can execute the below command
https://nodejs.medium.com/announcing-a-new-experimental-modules-1be8d2d6c2ff

node --experimental-modules --es-module-specifier-resolution=node index.js

We can add this into package.json and just type npm start.

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type":"module",
  "scripts": {
    "start": "node --experimental-modules --es-module-specifier-resolution=node index.js"
  },
  "author": "",
  "license": "ISC"
}
JavaScriptNodeJsTypeScript

Comments (0)

Discussions (0)


© 2021 dsabyte. All rights reserved