为了账号安全,请及时绑定邮箱和手机立即绑定

无法将类导入 index.js 文件

无法将类导入 index.js 文件

茅侃侃 2023-04-27 16:36:42
225/5000你好,尽管我可以在 Internet 上找到所有信息,但我无法将类导入到我的 index.js 文件中。索引.js:import {Brandade} from "./modules/Brandade";const brandade = new Brandade('ma brandade',5);布兰达德.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);    }}我收到此错误: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'}我的 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"  }}你能指引我走向光明吗?先感谢您。
查看完整描述

3 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

从你的 package.json 我可以看到你正在尝试使用 --experimental-modules 运行节点。如果是,请尝试更改:

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

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

运行 node--experimental-modules本身不会解析文件扩展名。


查看完整回答
反对 回复 2023-04-27
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

尝试使用 commonjs 导入样式。


代替


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

经过


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

同时将 Brandade 文件导出语法更改为 commonjs,如下所示。


class Brandade {

  constructor(){


  }

}


module.exports = { Brandade };

为了使用 ES6 导入语法,请使用像 Babel 这样的转译器。


查看完整回答
反对 回复 2023-04-27
?
RISEBY

TA贡献1856条经验 获得超5个赞

我的 nodejs 版本是 14,我回到了 12 版本。


节点--版本


v12.19.0



npm --version


6.14.8



我删除了 package.json 并输入命令“npm init”。已创建一个新的 package.json 文件。我添加了以下行:


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

我还修改了 Brandade.js 文件:


Module.exports = {Brandade};

在 :


Module.exports = Brandade;

最后,在 index.js 中,我添加了以下几行:


let Brandade = require ('./ scripts / Brandade');

console.log ("Hello");

let brandade = new Brandade ("my name", 3);

brandade.show_name ();

而且效果很好。


感谢您的参与和回答。


再见。


有关信息,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"

}


布兰达德.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 ;


查看完整回答
反对 回复 2023-04-27
  • 3 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信