简体   繁体   English

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

[英]Node.JS cannot find json file, everything is right

When I'm using my test application, I get this error:当我使用我的测试应用程序时,我收到此错误:

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' ]
}

My code is:我的代码是:

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!')
})

This is my directory image:这是我的目录图像:

目录

Hopefully someone could answer and save me and other people's time.希望有人可以回答并节省我和其他人的时间。

My OS:我的操作系统:

Windows 64 Bit Windows Home 10 Node.JS Version 12.8.1 LTS Visual Studio Code (not sure if that matters) Windows 64 位 Windows 主页 10 Node.JS 版本 12.8.1 LTS Visual Studio 代码(不确定是否重要)

const details = require('d.json')

everything is right一切都是对的

No, it isn't.不,不是。 Try using an explicit path relative to the working directory, like this:尝试使用相对于工作目录的显式路径,如下所示:

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

From 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. require 在哪里找到文件的规则可能有点复杂,但一个简单的经验法则是,如果文件不是以“./”或“/”开头,那么它要么被视为核心模块(并且本地 Node.js 路径已检查),或本地 node_modules 文件夹中的依赖项。 If the file starts with "./" it is considered a relative file to the file that called require.如果文件以“./”开头,则它被认为是调用 require 的文件的相对文件。

Check out the following link查看以下链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM