javascript - 无法将类导入 index.js 文件
问题描述
225/5000
你好,
尽管我可以在 Internet 上找到所有信息,但我无法将类导入我的 index.js 文件。
index.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";
运行 node with--experimental-modules
不会自行解析文件扩展名。您可以在此处阅读有关此内容的更多信息
推荐阅读
- typescript - 为什么这个对象的类型解析到类型联合的一侧?
- python - 根据条件提取行 - Pandas
- java - 从 Java 执行终端命令而不做任何更改
- data-cleaning - Open Refine 显示空白蓝屏,我无法上传任何数据进行清理。为什么会发生这种情况,我该如何解决?
- r - r 中的 if 和 else 问题,得到的结果是我给出的选项中不存在的数字
- c# - .Net application crashes despite being inside try catch block C#
- python - debug python versions < 3.5 with vscode new debugger
- python - 如何在另一个字典中创建字典?
- angular - Angular Upgradation Version 8 To Version 9. Purpose Of --create-commits Option
- django - Align Bootstrap Tags with Text in List