Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Rotes in Nodejs

Belan

Coder
Hello!

I´m doing a rote to search but it´isnt work
Code:
C:\Users\arifu\ntask-api\node_modules\consign\lib\consign.js:121

      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\ar\ntask-api\models\tasks.js from C:\Users\ar\ntask-api\node_modules\consign\lib\consign.js not supported.
tasks.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename tasks.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\Users\ar\ntask-api\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.newLoader [as .js] (C:\Users\ar\ntask-api\node_modules\pirates\lib\index.js:121:7)
    at Consign.into (C:\Users\ar\ntask-api\node_modules\consign\lib\consign.js:232:15)
    at file:///C:/Users/ar/ntask-api/index.js:14:3 {
  code: 'ERR_REQUIRE_ESM'
}

JSON:
{
  "name": "ntask-api",
  "version": "1.0.0",
  "description": "API de gestão de tarefas",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "babel-node index.js"
  },
  "author": "Maria Izabel",
  "dependencies": {
    "@babel/cli": "^7.0.0",
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "consign": "^0.1.6",
    "express": "^4.18.2"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/node": "^7.0.0",
    "@babel/preset-env": "^7.23.3"
  }
}

index.js
JavaScript:
import express from 'express';
import consign from 'consign';

const app = express();
app.set("json spaces", 4)
consign()
 .include("models")
 .then("libs/middlewares.js")
 .then("routes")
 .then("libs/boot.js")
 .into(app);

tasks.js
JavaScript:
module.exports = app => {
    return{
        findAll:(params, callback) =>{
            return callback([
                {title: "Fazer Compras"},
                {title: "Consertar o pc"},
            ]);
        }
    };
};
 
Last edited by a moderator:
Hello 👋

To confirm,
JavaScript:
Code:
module.exports = app => {
    return{
        findAll:(params, callback) =>{
            return callback([
                {title: "Fazer Compras"},
                {title: "Consertar o pc"},
            ]);
        }
    };
};
this is the whole file? Do you have any 'require' statements in this file?
 
It is in this file index.js main

JavaScript:
const tasks = require("../models/tasks");
module.exports = app =>{
    app.get("/", (req, res) =>{
        res.json({status: "NTask API"});
    });
};
module.exports = app => {
 const Tasks = app.models.tasks;  
 app.get("/tasks", (req,res) => {
    Tasks.findAll({}, (tasks) => {
     res.json({tasks: tasks});       
    });
 });
};
 
Last edited by a moderator:
It is in this file index.js main

const tasks = require("../models/tasks");
module.exports = app =>{
app.get("/", (req, res) =>{
res.json({status: "NTask API"});
});
};
module.exports = app => {
const Tasks = app.models.tasks;
app.get("/tasks", (req,res) => {
Tasks.findAll({}, (tasks) => {
res.json({tasks: tasks});
});
});
};
I believe it’s the first line: ‘const tasks = require("../models/tasks");’

You would have to change it for an import statement. Something like import { myfunction} from "./where-my-function-is.js";.
 
Last edited:

New Threads

Buy us a coffee!

Back
Top Bottom