【问题标题】:I am new to nodejs and i keep getting this error我是nodejs的新手,我一直收到这个错误
【发布时间】:2020-07-10 14:08:24
【问题描述】:

我不断收到此错误。我已经删除了 node.js 两次并重新安装。同样的错误不断出现。 error gotten from nodejs

下面是我试图导出到 index.js 文件的 person.js 文件:

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greeting() {
        console.log(`my name is ${this.name}`)
    }
}

module.exports = Person;

这是 index.js 文件:

const Person = require("./person")

const person1 = new Person ("Philippa", 30)

person1.greeting()

另外,我试过这样做,但我一直收到同样的错误:

const path = require("path")

console.log(__filename)

console.log(__dirname)

近期编辑:我尝试引入 path 和 fs 模块,因为这是我现在使用 require() 处理的。下面仍然出现此错误:

enter code here
$ node path.js
internal/modules/cjs/loader.js:985
  throw err;
  ^

Error: Cannot find module 'C:\Users\9470M\Documents\node_crash_course\path.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

【问题讨论】:

标签: node.js path export node-modules throw


【解决方案1】:

您在文件person .js 中添加了空格。重命名为person.js

【讨论】:

  • 我希望我现在可以使用表情符号。我已经尝试了两天了。谢谢。
  • 答案只是解决了部分问题,因为我无法引入 path 和 fs 模块,因为这就是我现在正在使用的内容
  • pathfs 都包含在 node.js 中。您只需要像以前那样要求它们:const path = require("path")。检查链接文档以使用他们的 API
猜你喜欢
  • 2021-01-20
  • 2014-12-12
  • 2022-01-20
  • 2018-06-14
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2020-04-14
  • 2014-01-10
相关资源
最近更新 更多