0

I don't get the logic of the module conception in nodejs / javascript

Here is my file : circle.js

const circleArea = r => 3.14 * (r ** 2);
const squareArea = s => s * s;
export {circleArea, squareArea};

file testCircle.js

import { circleArea as circle, squareArea } from "./circle.js"
console.log(squareArea(2));

When I run the program node testCircle.js

SyntaxError: Cannot use import statement outside a module

I have found an explanation here :

SyntaxError: Cannot use import statement outside a module

So In have updated my package.json

 "type": "module"

And it works

BUT

then when I want to run another file which require :

const http = require("http");

I got this new error :

ReferenceError: require is not defined

Is there a way to work with both import and require ?

luk2302
57.5k24 gold badges109 silver badges152 bronze badges
asked Aug 1, 2022 at 7:16
5
  • Just use import instead of require. Commented Aug 1, 2022 at 7:18
  • Can you remove type="module" from 'package.json` and try again ? Commented Aug 1, 2022 at 7:20
  • @Jaood_xD Your comment is wrong and the link doesn't even mention ES6 modules. Commented Aug 1, 2022 at 7:51
  • An example for a ES6 module importing an CJS module: stackblitz.com/edit/node-dar2r5?file=index.js Commented Aug 1, 2022 at 8:08
  • @jabaa my bad, wrong link Commented Aug 1, 2022 at 9:52

1 Answer 1

2

Use the extension mjs for ES6 modules without "type": "module" or cjs for Common JS modules with "type": "module". Then you can use both types of modules in one project.

Example 1:

package.json:

{
 "name": "node-starter",
 "version": "0.0.0",
 "scripts": {
 "start": "node index.js"
 },
 "type": "module"
}

index.js:

import { a } from './a.cjs';
a();

a.cjs:

module.exports = {
 a() {
 console.log('Hello World!');
 },
};

Running example: https://stackblitz.com/edit/node-dar2r5

Example 2:

package.json:

{
 "name": "node-starter",
 "version": "0.0.0",
 "scripts": {
 "start": "node index.mjs"
 }
}

index.mjs:

import { a } from './a.js';
a();

a.js:

module.exports = {
 a() {
 console.log('Hello World!');
 },
};

Running example: https://stackblitz.com/edit/node-enengw

answered Aug 1, 2022 at 8:13
Sign up to request clarification or add additional context in comments.

2 Comments

If I add const http = require("http") in your index.mjs, then I got an error : ReferenceError: require is not defined
@fransua A file is either a CommonJS module or a ES6 module. You can't use require in a ES6 module and you can't use import in a CommonJS module. That means you can't mix require and import in the same file. Use import http from "http"; instead

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.