[DEV] 기록

[Maven] The cb argument must be of type function received undefined

꾸준함. 2023. 1. 27. 15:13

개요

maven package 명령어를 작성하는데 아래와 같은 에러가 lib/modules/mvn/target.js 파일에서 아래 에러가 발생했습니다.

 

TypeError ERR_INVALID_ARG_TYPE: The cb argument must be of type function received undefined

 

원인

maven 버전과 java 버전 호환성이 맞지 않아 발생하는 원인인 것 같습니다.

기존 파일에서는 fs.makedir() 메서드가 콜백 메서드가 아니어서 발생하는 문제였습니다.

 

기존 파일

 

var path = require('path');
var fs = require('fs');
const config = require('./package');
const http = require("http");
var extract = require('extract-zip');

let defaultUrl = `http://www-us.apache.org/dist/maven/maven-3/${config.mavenVersion}/binaries/apache-maven-${config.mavenVersion}-bin.zip`;
let fileZipName = path.resolve(__dirname, 'src', `apache-maven-${config.mavenVersion}-bin.zip`);
let target = exports;
target.home = process.env.HOME || process.env.USERPROFILE;
target.home += '/.maven';
target.path = path.join(target.home, `apache-maven-${config.mavenVersion}`, 'bin');
target.pathCheck = path.join(target.path, 'mvn');

target.init = function() {
  let nothing = true;
  // Make dir .maven
  try {
    fs.accessSync(target.pathCheck, fs.F_OK);
    nothing = false;
  } catch (e) {
    try {
      fs.accessSync(target.home, fs.F_OK);
    } catch (e) {
      fs.mkdir(target.home);
    }
  }
  // install maven
  if (nothing) {
    createFolder();
  }
}

function createFolder() {
  file = fs.createReadStream(fileZipName);
  extract(fileZipName, {dir: target.home}, function (err) {});
}

target.init();

 

해결 방법

fs.makedir 메서드를 콜백 메서드로 변경하면 해결이 됩니다.

저 같은 경우 아래와 같이 변경해서 해결했습니다.

 

var path = require('path');
var fs = require('fs');
const config = require('./package');
const http = require("http");
var extract = require('extract-zip');

let defaultUrl = `http://www-us.apache.org/dist/maven/maven-3/${config.mavenVersion}/binaries/apache-maven-${config.mavenVersion}-bin.zip`;
let fileZipName = path.resolve(__dirname, 'src', `apache-maven-${config.mavenVersion}-bin.zip`);
let target = exports;
target.home = process.env.HOME || process.env.USERPROFILE;
target.home += '/.maven';
target.path = path.join(target.home, `apache-maven-${config.mavenVersion}`, 'bin');
target.pathCheck = path.join(target.path, 'mvn');

target.init = function() {
  let nothing = true;
  // Make dir .maven
  try {
    fs.accessSync(target.pathCheck, fs.F_OK);
    nothing = false;
  } catch (e) {
    try {
      fs.accessSync(target.home, fs.F_OK);
    } catch (e) {
      fs.mkdir(target.home, function () {

      });
    }
  }
  // install maven
  if (nothing) {
    createFolder();
  }
}

function createFolder() {
  file = fs.createReadStream(fileZipName);
  extract(fileZipName, {dir: target.home}, function (err) {});
}

target.init();

 

요약하자면 fs.makedir(target.home)을 fs.makedir(target.home, function() {});으로 변경해서 해결했습니다.

위와 같이 콜백 메서드로 변경할 경우 mvn package 명령어에서 발생하는 에러를 해결할 수 있습니다.

 

출처

https://stackoverflow.com/questions/62359078/nodejs-returns-type-error-when-creating-a-file-using-fs-writefile

 

nodeJS returns Type Error when creating a file using fs writeFile

I've just started learning nodeJS. I'm at a very basic level. Right now I'm working with creating new files and directories methods. This is my Code - var fs = require('fs'); fs.mkdir("stuff",

stackoverflow.com

 

반응형