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.

Rudy
  • 1
  • 1
  • 1
    What is your file structure? – mousetail Oct 09 '20 at 15:17
  • Did you check that your module bundler allows you to omit file extension at the end of your `import` statement ? – Romuald Oct 09 '20 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 https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node#:~:text=You%20can%20also%20use%20npm,import%20in%20your%20JS%20files. – Fraddy Oct 09 '20 at 16:46

3 Answers3

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

Nahush Farkande
  • 5,290
  • 3
  • 25
  • 35
  • Actually I've made a lot of changes but so far nothing is working. – Rudy Oct 12 '20 at 15:49
  • 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 – Nahush Farkande Oct 13 '20 at 04:41
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.

Fraddy
  • 331
  • 1
  • 8
  • 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](https://riptutorial.com/fr/javascript/example/15780/commencez-a-utiliser-es6---7-avec-babel). But nothing helps. I have another error appearing: `const brandade = require('./dist/scripts/Brandade'); ^ ReferenceError: require is not defined ` – Rudy Oct 12 '20 at 15:47
  • There may be some issue with import path or Babel integration. Your question is also not updated. Try to refer https://babeljs.io/en/setup#installation , section "Babel built-ins" => "CLI" – Fraddy Oct 12 '20 at 16:28
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 ;
Rudy
  • 1
  • 1