[英]Cannot import class into index.js file
225/5000
你好,
尽管我可以在 Internet 上找到所有信息,但我无法将类导入到我的 index.js 文件中。
索引.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);
}
}
我收到此错误:
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"
}
}
你能引导我走向光明吗?
先感谢您。
从您的 package.json 我可以看到您正在尝试使用 --experimental-modules 运行节点。 如果是,请尝试更改:
import {Brandade} from "./modules/Brandade";
到
import {Brandade} from "./modules/Brandade.js";
使用--experimental-modules
运行节点不会自行解析文件扩展名。 您可以在此处阅读有关此内容的更多信息
尝试使用 commonjs 导入样式。
代替
import {Brandade} from "./modules/Brandade";
经过
const { Brandade } = require("./modules/Brandade");
还将 Brandade 文件导出语法更改为 commonjs,如下所示。
class Brandade {
constructor(){
}
}
module.exports = { Brandade };
为了使用 ES6 导入语法,请使用像 Babel 这样的转换器。
我的 nodejs 版本是 14,我回到了 12 版本。
节点 --version
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"
}
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.