19

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!

6
  • 49
    Use import Queue from "./lib/Queue.js";, including the file extension Commented Feb 4, 2020 at 14:09
  • @Bergi, did not work for me Commented Apr 28, 2021 at 13:33
  • @SunilGarg You might want to ask a new question 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. Commented Apr 28, 2021 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 Commented Apr 29, 2021 at 0:32
  • 1
    specifying the filename extension as ".mjs" worked for me. Commented Sep 18, 2021 at 1:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.