Nội dung bài viết Node.js cung cấp một module path nhằm mục đích xử lý đường dẫn file và folder. Tất nhiên là hiệu suất mang lại mỗi hệ điều hành là khác nhau. Đây là bài hướng dẫn sử dụng Path trong NodeJS, tương đối đầy đủ, nếu còn thiếu điều bạn cần tìm vui lòng truy cập vào Module path Nodejs để biết thêm chi tiết.Video học lập trình mỗi ngày
Lấy đường dẫn của thư mục
const path = require['path']
path.dirname['/path/anonystick/index.js'] // /path/anonystick
Lấy phần mở rộng của đường dẫn
const path = require['path']
path.extname['/path/anonystick/index.js'] // .js
Kiểm tra có phải là một đường tuyệt đối hay không?
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
Nối các đường dẫn
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
Phân giải chuỗi đường dẫn thành đường dẫn tuyệt đối
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
Đường dẫn chuẩn hóa
path.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
parse path
path.parse['/path/anonystick/index.js']
/*
{ root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index' }
*/
Đường dẫn tuần tự hóa
path.format[{
root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index'
}] // /path/anonystick/index.js
Trên đây là những gì bạn cần để hiểu về Module path trong node.js. Bạn có thể tìm hiểu và học về Node.js có nhiều bài viết ở Series - Nodejs
Hết.
Nội dung bài viết Nội dung chính
Video học lập trình mỗi ngày
Node.js cung cấp một module path nhằm mục đích xử lý đường dẫn file và folder. Tất nhiên là hiệu suất mang lại mỗi hệ điều hành là khác nhau.
Đây là bài hướng dẫn sử dụng Path trong NodeJS, tương đối đầy đủ, nếu còn thiếu điều bạn cần tìm vui lòng truy cập vào Module path Nodejs để biết thêm chi tiết.
Lấy đường dẫn của thư mục
const path = require['path']
path.dirname['/path/anonystick/index.js'] // /path/anonystick
Lấy phần mở rộng của đường dẫn
const path = require['path']
path.extname['/path/anonystick/index.js'] // .js
Kiểm tra có phải là một đường tuyệt đối hay không?
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
Nối các đường dẫn
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
Phân giải chuỗi đường dẫn thành đường dẫn tuyệt đối
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
Đường dẫn chuẩn hóa
path.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
parse path
path.parse['/path/anonystick/index.js']
/*
{ root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index' }
*/
Đường dẫn tuần tự hóa
path.format[{
root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index'
}] // /path/anonystick/index.js
Trên đây là những gì bạn cần để hiểu về Module path trong node.js. Bạn có thể tìm hiểu và học về Node.js có nhiều bài viết ở Series - Nodejs
Hết.
How would I get the path to the script in Node.js?
I know there's
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
7, but that only refers to the directory where the script was called, not of the script itself. For instance, say I'm in path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
8 and I run the following command:const path = require['path']
path.extname['/path/anonystick/index.js'] // .js
6If I call
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
9, I get path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
8, not path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
1. Is there a way to get that directory?asked Jun 28, 2010 at 14:31Jun 28, 2010 at 14:31
Kyle SlatteryKyle SlatteryKyle Slattery
31.4k9 gold badges31 silver badges36 bronze badges9 gold badges31 silver badges36 bronze badges
1
I found it after looking through the documentation again. What I was looking for were the
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
2 and path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 module-level variables.
2 is the file name of the current module. This is the resolved absolute path of the current module file. [ex:path.resolve['/foo/bar', './baz']// '/foo/bar/baz' path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file' path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
5]path.resolve['/foo/bar', './baz']// '/foo/bar/baz' path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file' path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 is the directory name of the current module. [ex:path.resolve['/foo/bar', './baz']// '/foo/bar/baz' path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file' path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
7]path.resolve['/foo/bar', './baz']// '/foo/bar/baz' path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file' path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
doom
2,9104 gold badges24 silver badges39 bronze badges4 gold badges24 silver badges39 bronze badges
answered Jun 28, 2010 at 14:39Jun 28, 2010 at 14:39
Kyle SlatteryKyle SlatteryKyle Slattery
31.4k9 gold badges31 silver badges36 bronze badges9 gold badges31 silver badges36 bronze badges
10
I found it after looking through the documentation again. What I was looking for were the
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
2 and path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 module-level variables.const path = require['path']
path.extname['/path/anonystick/index.js'] // .js
7path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
2 is the file name of the current module. This is the resolved absolute path of the current module file. [ex:path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
5]path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 is the directory name of the current module. [ex:path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
7]const path = require['path']
path.extname['/path/anonystick/index.js'] // .js
8doom
const path = require['path']
path.extname['/path/anonystick/index.js'] // .js
92,9104 gold badges24 silver badges39 bronze badgesSep 8, 2011 at 18:40
8
answered Jun 28, 2010 at 14:39
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
0So basically you can do this:
Use resolve[] instead of concatenating with '/' or '\' else you will run into cross-platform issues.
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
1//nodejs.org/api/modules.html#modules_dirname
Note: __dirname is the local path of the module or included script. If you are writing a plugin which needs to know the path of the main script it is:
or, to just get the folder name:Dec 22, 2017 at 14:30
Playdome.ioPlaydome.ioPlaydome.io
answered Sep 8, 2011 at 18:402 gold badges15 silver badges30 bronze badges
2
Use __dirname!!
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
2The directory name of the current module. This the same as the path.dirname[] of the
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
2.Example: running node example.js from /Users/mjrFor ESModules you would want to use:
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
9answered Dec 22, 2017 at 14:301 gold badge16 silver badges19 bronze badges
2,9212 gold badges15 silver badges30 bronze badgesOct 23, 2016 at 7:17
This command returns the current directory:Masoud Siahkali
For example, to use the path to read the file:1 gold badge26 silver badges18 bronze badges
5
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
3dYale
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
41,4431 gold badge16 silver badges19 bronze badges
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
5answered Oct 23, 2016 at 7:17Apr 27, 2018 at 1:01
Masoud SiahkaliMasoud SiahkaliGOTO 0
4,6851 gold badge26 silver badges18 bronze badges21 gold badges110 silver badges141 bronze badges
5
Node.js 10 supports ECMAScript modules, where
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 and path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
2 are no longer available.const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
6Then to get the path to the current ES module one has to use:
process.argv
And for the directory containing the current module:the second element will be the path to the JavaScript file. The next elements will be any additional command line arguments.
answered Apr 27, 2018 at 1:01
GOTO 0GOTO 0Dec 17, 2015 at 10:41
36.9k21 gold badges110 silver badges141 bronze badgesLukasz Wiktor
When it comes to the main script it's as simple as:4 gold badges68 silver badges81 bronze badges
4
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
7From the Node.js documentation:Nov 5, 2012 at 5:41
An array containing the command line arguments. The first element will be 'node', the second element will be the path to the JavaScript file. The next elements will be any additional command line arguments.foobar
If you need to know the path of a module file then use __filename.3 silver badges2 bronze badges
2
answered Dec 17, 2015 at 10:41
Lukasz WiktorLukasz Wiktor
18.9k4 gold badges68 silver badges81 bronze badges4 gold badges31 silver badges58 bronze badges
answered Nov 5, 2012 at 5:41May 25, 2016 at 21:27
2
foobarfoobar
const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
8Mike
Huy hiệu vàng 13K2626 gold badges90 silver badges148 bronze badges
Đã trả lời ngày 15 tháng 3 năm 2017 lúc 15:37Mar 15, 2017 at 15:37
Dana Harrisdana HarrisDana Harris
3773 Huy hiệu bạc6 Huy hiệu Đồng3 silver badges6 bronze badges
0
Bạn có thể sử dụng Process.Env.PWD để có đường dẫn thư mục ứng dụng hiện tại.
Đã trả lời ngày 28 tháng 9 năm 2016 lúc 7:00Sep 28, 2016 at 7:00
AbisivamabisivamAbiSivam
4211 Huy hiệu vàng6 Huy hiệu bạc17 Huy hiệu đồng1 gold badge6 silver badges17 bronze badges
1
Nếu bạn đang sử dụng
path.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
5 để đóng gói ứng dụng của mình, bạn sẽ thấy hữu ích biểu thức này:const path = require['path']
path.isAbsolute['/path/anonystick/index.js'] // true
path.isAbsolute['.'] // false
9
6 cho biết nếu ứng dụng đã được đóng gói bởipath.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
5.path.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
8 giữ đường dẫn đầy đủ của thực thi, làpath.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
9 hoặc tương tự cho các yêu cầu trực tiếp của các tập lệnh [path.normalize['/path///anonystick/index.js'] // /path/anonystick/index.js
0] hoặc ứng dụng được đóng gói.path.parse['/path/anonystick/index.js'] /* { root: '/', dir: '/path/anonystick', base: 'index.js', ext: '.js', name: 'index' } */
1 giữ đường dẫn đầy đủ của tập lệnh chính, nhưng nó trống khi nút chạy ở chế độ tương tác.path.parse['/path/anonystick/index.js'] /* { root: '/', dir: '/path/anonystick', base: 'index.js', ext: '.js', name: 'index' } */
3 giữ đường dẫn đầy đủ của tập lệnh hiện tại, vì vậy tôi không sử dụng nó [mặc dù nó có thể là điều OP yêu cầu; sau đó sử dụng tốt hơnpath.resolve['/foo/bar', './baz']// '/foo/bar/baz' path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file' path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 lưu ý rằng ở chế độ tương tácpath.parse['/path/anonystick/index.js'] /* { root: '/', dir: '/path/anonystick', base: 'index.js', ext: '.js', name: 'index' } */
3 trống.path.resolve['/foo/bar', './baz']// '/foo/bar/baz' path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file' path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
Đối với chế độ tương tác, sử dụng
5 để đưa đường dẫn đến nút thực thi hoặcpath.parse['/path/anonystick/index.js'] /* { root: '/', dir: '/path/anonystick', base: 'index.js', ext: '.js', name: 'index' } */
9 để có được thư mục hiện tại.path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
Đã trả lời ngày 8 tháng 9 năm 2017 lúc 7:28Sep 8, 2017 at 7:28
DMContadordMContadordmcontador
5901 Huy hiệu vàng6 Huy hiệu bạc18 Huy hiệu đồng1 gold badge6 silver badges18 bronze badges
0
Sử dụng phương thức
path.parse['/path/anonystick/index.js']
/*
{ root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index' }
*/
7 của mô -đun path.parse['/path/anonystick/index.js']
/*
{ root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index' }
*/
8:path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
0Dưới đây là tài liệu ví dụ trên được lấy từ.
Như Dan đã chỉ ra, Node đang làm việc trên các mô-đun Ecmascript với cờ "-mô-đun-Experimental". Node 12 vẫn hỗ trợ
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3 và path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
2 như trên.Nếu bạn đang sử dụng cờ
path.format[{
root: '/',
dir: '/path/anonystick',
base: 'index.js',
ext: '.js',
name: 'index'
}] // /path/anonystick/index.js
1, có một cách tiếp cận khác.Thay thế là để đưa đường dẫn đến mô -đun ES hiện tại:
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
1Và đối với thư mục chứa mô -đun hiện tại:
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
2PJ Eby
8,5365 Huy hiệu vàng22 Huy hiệu bạc18 Huy hiệu đồng5 gold badges22 silver badges18 bronze badges
Đã trả lời ngày 26 tháng 4 năm 2019 lúc 1:08Apr 26, 2019 at 1:08
Michael Colemichael ColeMichael Cole
Huy hiệu vàng 14,6K55 gold badges72 silver badges91 bronze badges
1
NodeJS phơi bày một biến toàn cầu gọi là
path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3.path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
3.
3 Trả về đường dẫn đầy đủ của thư mục nơi tệp JavaScript cư trú.path.resolve['/foo/bar', './baz']// '/foo/bar/baz'
path.resolve['/foo/bar', '/tmp/file/']// '/tmp/file'
path.resolve['www', 'static_files/png/', '../gif/image.gif'] //'/home/anonystick/node/www/static_files/gif/image.gif'
Vì vậy, như một ví dụ, đối với Windows, nếu chúng ta tạo một tệp tập lệnh với dòng sau:
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
3Và chạy tập lệnh đó bằng cách sử dụng:
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
4Đầu ra sẽ là: C: \ Người dùng ... \ InfnerFolder1 \ InfnerFolder2 \ InfnerFolder3C:\Users...\innerFolder1\innerFolder2\innerFolder3
Đã trả lời ngày 2 tháng 4 lúc 11:36Apr 2 at 11:36
Idan_KrupnikIdan_KrupnikIdan_Krupnik
3231 Huy hiệu vàng4 Huy hiệu bạc5 huy hiệu đồng1 gold badge4 silver badges5 bronze badges
1
index.js trong bất kỳ thư mục nào chứa các mô -đun để xuất
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
5Điều này sẽ tìm thấy tất cả các tệp trong gốc của thư mục hiện tại, yêu cầu và xuất mọi tệp có cùng tên xuất với gốc tên tệp.
Đã trả lời ngày 17 tháng 1 năm 2021 lúc 22:53Jan 17, 2021 at 22:53
Andy Lorenzandy LorenzAndy Lorenz
2.6541 Huy hiệu vàng26 Huy hiệu bạc27 Huy hiệu đồng1 gold badge26 silver badges27 bronze badges
Nếu bạn muốn một cái gì đó giống như $ 0 trong tập lệnh shell, hãy thử điều này:
path.join['/path', 'anonystick', './index.js'] // /path/anonystick/index.js
6Đã trả lời ngày 5 tháng 5 năm 2017 lúc 9:32May 5, 2017 at 9:32
1