Node.JS 找不到 json 文件,一切正常

Node.JS cannot find json file, everything is right

当我使用测试应用程序时,出现此错误:

C:\Users\****\Downloads\noded.js website tetstst>node app.js
internal/modules/cjs/loader.js:985
  throw err;
  ^

Error: Cannot find module 'd.json'
Require stack:
- C:\Users\*****\Downloads\noded.js website tetstst\app.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\Users\****\Downloads\noded.js website tetstst\app.js:4:17)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\Users\*****\Downloads\noded.js website tetstst\app.js' ]
}

我的代码是:

const express = require('express')
const app = express()
const fs = require('fs')
const details = require('d.json')
const bcrypt = require('bcrypt')
app.set('view engine', 'ejs')

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', function (req, res) {
  res.render('index')
})

app.post('/', function (req, res) {
    res.render('index');
    const email = req.body.email
    const username = req.body.username
    if(!details[username]) {
        details[username] = {
            "email": email,
            "password": "plain"
        }
        const password = bcrypt.genSalt(saltRounds, function(err, salt) {
            bcrypt.hash(req.body.password, salt, function(err, hash) {
                details[username].password = hash
            });
        });
    }else{
        if(details[username]){
            return res.render('index', {weather: 'Pass!' + username, error: null});
        }else{
            return res.render('index', {weather: 'No User Found For:' + username, error: null});
        }
    }
    
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

这是我的目录图片:

希望有人能回答并节省我和其他人的时间。

我的OS:

Windows64位Windows首页10 Node.JS 版本 12.8.1 LTS Visual Studio 代码(不确定这是否重要)

const details = require('d.json')

everything is right

不,不是。尝试使用相对于工作目录的显式路径,如下所示:

const details = require('./d.json')

来自the documentation

The rules of where require finds the files can be a little complex, but a simple rule of thumb is that if the file doesn't start with "./" or "/", then it is either considered a core module (and the local Node.js path is checked), or a dependency in the local node_modules folder. If the file starts with "./" it is considered a relative file to the file that called require.

查看以下内容link

const details = require('./d')
const data = require('path/to/file/filename');