Code Monkey home page Code Monkey logo

Comments (6)

rosskukulinski avatar rosskukulinski commented on September 16, 2024

hi @bbsalsero. What is the actual filename/extension of myfile?

require() does not need you to specify the .js or .json extensions, but readFile/readFileSync do.

So:

require('mylib') and fs.readFileSync('mylib.js') will both utilize the same file, while fs.readFileSync('mylib') will fail.

from help.

bbsalsero avatar bbsalsero commented on September 16, 2024

thanks for your reply,

let's be more specific:

var fs =  require('fs')

var root ='./modules/configs/'
var dir = fs.readdirSync(root)
console.log(dir)
var moduleConfig={}


for(e in dir){

console.log("####")
console.log(dir[e])


console.log(fs.readFileSync(root+dir[e]).toString())//// so this is working
 moduleConfig[dir[e]]= require(root+dir[e]) //// but this is not:(




console.log( moduleConfig[dir[e]])
console.log("####")
}



so the file name is dynamic but they are all json file for the record.

from help.

bbsalsero avatar bbsalsero commented on September 16, 2024

ok this is really odd,

ok let's be much more specific:

i'm loading a module that is loading it self some json file dynamically,

but even if i use the file full path it throw error.

like require('c:\list.jon')

Error : cannot find module 'c:\list.jon'

i'm on windows 10 and nodejs 0.12.7

from help.

bbsalsero avatar bbsalsero commented on September 16, 2024

ooooooooookay, i'm using node since v 0.5 but i've never seen that;

so , i run myscript.js who load mymodules.js, in mymodules.js i put:

module.exports=function( ){
var list =  require(__filename)
console.log(__filename) // this gave me mymodule.js 
console.log(list.toString()) // this show me myscript.js
}

can anyone could explain it?

from help.

Slayer95 avatar Slayer95 commented on September 16, 2024

@bbsalsero, with Node 0.12.7@Win7x64 I get the source of mymodule.js with that last script.

The way it's intended to work, relative paths passed to fs work relative to the root of your project, while relative paths passed to require work relative to the folder containing the file calling require.

from help.

Knighton910 avatar Knighton910 commented on September 16, 2024

feel free to reopen, happy coding :)

from help.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.