0

225/5000

Hello,

Despite all the information I can find on the internet, I cannot import a class into my index.js file.

index.js:

import {Brandade} from "./modules/Brandade";
const brandade = new Brandade('ma brandade',5);

Brandade.js:

export class Brandade{
    nom: string;
    prix: number;

    constructor(nom: string, prix: number) {
        this.nom = nom;
        this.prix = prix;
    }

    get nom(){
        return this.nom;
    }

    set nom(nom: string){
        return this.nom = nom;
    }

    afficher_nom(){
        console.log(this.nom);
    }
}

I am getting this error:

internalBinding ('errors'). triggerUncaughtException (
                            ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C: \ Users \ user \ OneDrive \ programming \ nodejs \ projects \ typescript_tests \ modules \ Brandade' imported from C: \ Users \ user \ OneDrive \ programming \ nodejs \ projects
\ typescript_tests \ index.js

    at finalizeResolution (internal / modules / esm / resolve.js: 276: 11)
    at moduleResolve (internal / modules / esm / resolve.js: 699: 10)
    at Loader.defaultResolve [as _resolve] (internal / modules / esm / resolve.js: 810: 11)
    at Loader.resolve (internal / modules / esm / loader.js: 85: 40)
    at Loader.getModuleJob (internal / modules / esm / loader.js: 229: 28)
    at ModuleWrap. <anonymous> (internal / modules / esm / module_job.js: 51: 40)
    at link (internal / modules / esm / module_job.js: 50: 36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

My package.json :

{
  "name": "typescript_tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node --experimental-modules index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^14.11.8"
  }
}

Can you please guide me to the light?

Thank you in advance.

3
  • 1
    What is your file structure? Commented Oct 9, 2020 at 15:17
  • Did you check that your module bundler allows you to omit file extension at the end of your import statement ? Commented Oct 9, 2020 at 15:22
  • If you still need to use ES6 syntax for experimental purpose then you will have to use mjs as file extension. Check link stackoverflow.com/questions/45854169/…. Commented Oct 9, 2020 at 16:46

3 Answers 3

2

From your package.json I can see that you are trying to run node using --experimental-modules. If yes, Try changing:

import {Brandade} from "./modules/Brandade";

to

import {Brandade} from "./modules/Brandade.js";

Running node with --experimental-modules does not resolve file extensions on its own. You can read more about this here

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I've made a lot of changes but so far nothing is working.
Can you tell me more about what's not working, or perhaps update the question to reflect the problems you are facing. I tried out your code with node v12.14.0 and it worked on my end using the changes I have mentioned in this answer. Maybe you can ensure that you are using a more recent version of node
1

Try using commonjs import style.

Replace

import {Brandade} from "./modules/Brandade";

By

const { Brandade } = require("./modules/Brandade");

Also change Brandade file export syntax to commonjs like given below.

class Brandade {
  constructor(){

  }
}

module.exports = { Brandade };

In order to using ES6 import syntax use transpiler like Babel.

2 Comments

Before making my request, I modified a lot of things in particular the transformation of imports "" from into const "" = require but I had not thought of Babel. So I installed Babel and performed the tutorial below : tuto_babel. But nothing helps. I have another error appearing: const brandade = require('./dist/scripts/Brandade'); ^ ReferenceError: require is not defined
There may be some issue with import path or Babel integration. Your question is also not updated. Try to refer babeljs.io/en/setup#installation , section "Babel built-ins" => "CLI"
0

My version nodejs was in 14, I went back to version 12.

node --version

v12.19.0

npm --version

6.14.8

I deleted the package.json and typed the command "npm init". A new package.json file has been created. I added the line:

"Start": "node index.js" in scripts.

I have also modified the Brandade.js file:

Module.exports = {Brandade};

in :

Module.exports = Brandade;

And finally, in index.js, I added these lines:

let Brandade = require ('./ scripts / Brandade');
console.log ("Hello");
let brandade = new Brandade ("my name", 3);
brandade.show_name ();

And it works wonderfully.

Thank you for your involvement and your answers.

See you soon.

For information, the package.json file:

{
  "name": "typescript_tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Brandade.js :

class Brandade {

    constructor(nom, prix) {
        this.nom = nom;
        this.prix = prix;
    }

    get nom(){
        return this.nom;
    }

    set nom(nom){
        return this.nom = nom;
    }

    afficher_nom(){
        console.log(this.nom);
    }
}
module.exports = Brandade ;

Comments

Your Answer

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.