3 回答

TA貢獻1829條經驗 獲得超13個贊
從你的 package.json 我可以看到你正在嘗試使用 --experimental-modules 運行節點。如果是,請嘗試更改:
import?{Brandade}?from?"./modules/Brandade";
到
import?{Brandade}?from?"./modules/Brandade.js";
運行 node--experimental-modules
本身不會解析文件擴展名。

TA貢獻1852條經驗 獲得超1個贊
嘗試使用 commonjs 導入樣式。
代替
import {Brandade} from "./modules/Brandade";
經過
const { Brandade } = require("./modules/Brandade");
同時將 Brandade 文件導出語法更改為 commonjs,如下所示。
class Brandade {
constructor(){
}
}
module.exports = { Brandade };
為了使用 ES6 導入語法,請使用像 Babel 這樣的轉譯器。

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 ;
添加回答
舉報