3

I'm refactoring some code to ES6 pattern of import. I'm using node 13.7.0.

My package.json file has the "type": "module" at the top, however when I run my script I get

Error: Cannot find module /Users/otaviobonder/WebstormProjects/newTE/back/src/lib/Queue imported from /Users/otaviobonder/WebstormProjects/newTE/back/src/server.js

I run the script like node --require dotenv/config src/server.js

I don't get why. I'm importing queue as import Queue from "./lib/Queue"; in server.js, while my Queue.js file has a default export:

import Queue from "bull";
import redisConfig from "../config/redis";
import * as Sentry from "@sentry/node";

import * as jobs from "../jobs";

Sentry.init({ dsn: process.env.SENTRY_DSN });

const queues = Object.values(jobs).map(job => ({
    bull: new Queue(job.key, { redis: redisConfig }),
    name: job.key,
    handle: job.handle,
    options: job.options
}));

export default {
    queues,
    add(name, data) {
        const queue = this.queues.find(queue => queue.name === name);

        return queue.bull.add(data, queue.options);
    },
    empty(name) {
        const queue = this.queues.find(queue => queue.name === name);

        return queue.bull.empty();
    },
    process() {
        return this.queues.forEach(queue => {
            queue.bull.process(queue.handle);

            queue.bull.on("failed", (job, err) => {
                Sentry.captureException(err);
            });
        });
    }
};

Any help is appreciate, since I've already read many articles about node and ES6 and can't find a solution.

Thanks!

Otavio Bonder
  • 1,015
  • 1
  • 7
  • 19
  • 13
    Use `import Queue from "./lib/Queue.js";`, including the file extension – Bergi Feb 04 '20 at 14:09
  • @Bergi, did not work for me – Sunil Garg Apr 28 '21 at 13:33
  • @SunilGarg You might want to [ask a new question](https://stackoverflow.com/questions/ask) that includes your folder structure, your build configuration, and all the relevant modules with their `import` and `export` declarations, as well as the exact error message. – Bergi Apr 28 '21 at 13:36
  • Hei @Bergi this is a question with thousands of views. Change your comment to an answer so I can mark as the solution – Otavio Bonder Apr 29 '21 at 00:32

0 Answers0