Comments (6)
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.
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.
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.
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.
@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.
feel free to reopen, happy coding :)
from help.
Related Issues (20)
- How to verify the signed message against the signature with raw ec public key only HOT 1
- stream.pipeline adds many close listeners, which can easily lead to MaxListenersExceededWarning HOT 2
- Failed to cross-complie fully-static nodejs with arm-linux-gnueabihf complier HOT 4
- High load on node library when CPU profiling HOT 4
- http2.request is downloading data with a very slow speed, if the network latency is significant HOT 2
- ip package not updated in expected nodejs/npm versions HOT 1
- npm run dev command is not working HOT 3
- Unable to install node image due to gpg check failed HOT 3
- Embedding integration questions (thread, stdout, cwd) HOT 4
- Cannot test locally committed code to Node.js in my PC. HOT 8
- "-sh: ./node: not found" on ARM platform HOT 1
- How to Enable Fast API Calls in Embedded Development HOT 1
- TENTANDO INSTALAR O YARN HOT 1
- Using official guide - Failed to install using 'fnm' CLI command on Win11 using PowerShell 7.4.4 HOT 3
- Asserting a range with descriptive error messages HOT 15
- [node:test] help using node api for node:test's run method with coverage report HOT 1
- Node-red is using the wrong version of nodejs and failing to start HOT 1
- WebSocket Data Transfer Slows Down After Node.js 16.14.2 Release HOT 21
- "# Fatal error in , line 0" when trying to run Node app from Rsyslog HOT 27
- tlsSocket.servername is false (sometimes) HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from help.