亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法將類導入 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 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號