The file name and full directory path can be accessed on the global object. Node.js also implements CommonJS, as a solution for loading external modules with the global require function. In this video, learn how you can access information about the current file and directory, as well as how to load core modules using the require function.
- In the last lesson,…we created and ran our first Node.js file.…We also learned about the global…which contains the functionality…that is available to us within a Node.js file,…without having to include…or require any other modules.…In this lesson we're going to continue…to explore this global object.…I'm going to come over here into the file folder,…and add a new file called global.js.…And another thing that's available to us globally,…is the name of the current file…as well as the full path to the directory…that we're currently using.…So console.log(__dirname);…is going to give us our directory name,…and console.log(__filename);…is going to give us the full name and path…to the current file.…
So I'm going to go ahead and open up the terminal,…and run this file by typing…node global.…And when we run this…we can see logged to the console…the full path to the directory that we are using,…as well as the full path to the directory…including the filename of the current file…that we've run,…global.js.…Node.js also comes with some tools…
- What is Node.js?
- Installing Node.js
- The global object
- Importing the core modules
- Creating custom events with the EventEmitter
- Reading, writing, and removing files
- Working with file streams
- Creating child processes with the exec and spawn functions
Skill Level Intermediate
Node.js: Real-Time Web with Socket.IOwith Emmanuel Henri1h 24m Intermediate
1. What Is Node.js?
2. Set Up Your Environnment
3. Node Globals
4. Node Modules
5. File System Basics
6. Files and Streams
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.