简体   繁体   English

无法将类导入 index.js 文件

[英]Cannot import class into index.js file

225/5000 225/5000

Hello,你好,

Despite all the information I can find on the internet, I cannot import a class into my index.js file.尽管我可以在 Internet 上找到所有信息,但我无法将类导入到我的 index.js 文件中。

index.js:索引.js:

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

Brandade.js: 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 :我的 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.先感谢您。

From your package.json I can see that you are trying to run node using --experimental-modules.从您的 package.json 我可以看到您正在尝试使用 --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.使用--experimental-modules运行节点不会自行解析文件扩展名。 You can read more about this here您可以在此处阅读有关此内容的更多信息

Try using commonjs import style.尝试使用 commonjs 导入样式。

Replace代替

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

By经过

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

Also change Brandade file export syntax to commonjs like given below.还将 Brandade 文件导出语法更改为 commonjs,如下所示。

class Brandade {
  constructor(){

  }
}

module.exports = { Brandade };

In order to using ES6 import syntax use transpiler like Babel.为了使用 ES6 导入语法,请使用像 Babel 这样的转换器。

My version nodejs was in 14, I went back to version 12.我的 nodejs 版本是 14,我回到了 12 版本。

node --version节点 --version

v12.19.0 v12.19.0

npm --version npm --version

6.14.8 6.14.8

I deleted the package.json and typed the command "npm init".我删除了 package.json 并输入了命令“npm init”。 A new package.json file has been created.已创建新的 package.json 文件。 I added the line:我添加了这一行:

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

I have also modified the Brandade.js file:我还修改了 Brandade.js 文件:

Module.exports = {Brandade};

in :在 :

Module.exports = Brandade;

And finally, in index.js, I added these lines:最后,在 index.js 中,我添加了以下几行:

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:有关信息,package.json 文件:

{
  "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 : 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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM