Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: module is not defined

So i have been trying to run this web app and at first it showed

(node:12960) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
C:\Users\J\react-messenger\stream-chat-boilerplate-api\src\index.js:1
import dotenv from 'dotenv'; ^^^^^^

SyntaxError: Cannot use import statement outside a module

And then i went to put set the type: module in the package.json but it gave me this error

ReferenceError: module is not defined

at file:///C:/Users/J/react-messenger/stream-chat-boilerplate-api/src/index.js:38:1

Here is my code:

import dotenv from 'dotenv';
dotenv.config();

import fs from 'fs';
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';

const api = express();

api.use(cors());
api.use(compression());
api.use(helmet());
api.use(bodyParser.urlencoded({ extended: true }));
api.use(bodyParser.json());

api.listen(process.env.PORT, error => {
    if (error) {
        console.warn(error);
        process.exit(1);
    }

    // eslint-disable-next-line array-callback-return
    fs.readdirSync(path.join(__dirname, 'routes')).map(file => {
        require('./routes/' + file)(api);
    });

    console.info(
        `Running on port ${process.env.PORT} in ${
            process.env.NODE_ENV
        } mode. 🚀`
    );
});

module.exports = api;

I dont know what am doing wrong

like image 649
ccurves Avatar asked Sep 20 '20 03:09

ccurves


People also ask

How do you fix the error require is not defined in node JS?

You are using require in a Node. If that is set to module , ES6 modules will be enabled and you will run into the error mentioned above (specifically ReferenceError: require is not defined in ES module scope, you can use import instead ). Simply remove that entry and you will be able to use require .

Is not defined in ES module scope?

The error "Module is not defined in ES module scope" occurs when we try to use the module. exports CommonJS syntax in ES modules. To solve the error, use the export keyword to export a member from a file, e.g. export const num = 42 .

What is require () in JavaScript?

require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.

Is an ES module file a .JS file?

By default, files with the . js extension will be treated as CommonJS modules, while files with the . mjs extension are treated as ES Modules. However, you might want to configure your NodeJS project to use ES Modules as the default module system.


1 Answers

You are mixing ES imports with CommonJS - at bottom of file you have module.exports = api; which is CJS terminology. The ES module equivalent is:

export default 
like image 127
cyberwombat Avatar answered Sep 21 '22 02:09

cyberwombat