process.chdir() 메소드는 Node.js 프로세스의 현재 디렉토리를 변경하는 데 사용됩니다. 오류가 발생하거나 프로세스가 실패하면 예외를 throw하지만 성공 시 응답을 반환하지 않습니다. 예:지정된 디렉토리가 없을 경우 실패할 수 있습니다.
구문
process.chdir(directory)
매개변수
-
디렉토리 – 여기에는 이전 디렉터리 이름 대신 업데이트될 디렉터리 이름이 포함됩니다.
예시
이름이 chdir.js인 파일을 만들고 아래 코드 조각을 복사합니다. 파일을 생성한 후 아래의 &Minus;
예제와 같이 이 코드를 실행하려면 다음 명령을 사용하십시오.node chdir.js
chdir.js
// Node.js program to demonstrate the use of process.chdir()
// Importing the process module
const process = require('process');
// Printing present working Directory
console.log("Present working directory: " + process.cwd());
try {
// Updating with the New directory
process.chdir('../tutorialspoint');
console.log("Updated working directory is: " + process.cwd());
} catch (err) {
// Printing error if any occurs
console.error("error occured while " + "changing directory: " + err);
} 출력
C:\home\node>> node chdir.js Present working directory: /home/mayankaggarwal/mysql-test Updated working directory is: /home/mayankaggarwal/tutorialspoint
예시
예를 하나 더 살펴보겠습니다.
// Node.js program to demonstrate the use of process.argv
// Importing the process module
const process = require('process');
try {
// Changing the directory with below namey
process.chdir('../not/tutorialspoint');
console.log("New Directory has been succesfully updated");
} catch (err) {
// Printing error if occurs
console.error("Error while changing directory", err);
} 출력
C:\home\node>> node chdir.js
Error while changing directory { Error: ENOENT: no such file or directory,
chdir '../not/tutorialspoint'
at process.chdir (internal/process/main_thread_only.js:31:12)
at Object.<anonymous> (/home/mayankaggarwal/mysql-test/process.js:9:9)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
errno: -2,
code: 'ENOENT',
syscall: 'chdir',
path: '../not/tutorialspoint' }