diff --git a/crates/taro_init/src/project.rs b/crates/taro_init/src/project.rs index 6632b4046883..ff94e66d7ecf 100644 --- a/crates/taro_init/src/project.rs +++ b/crates/taro_init/src/project.rs @@ -153,7 +153,7 @@ impl Project { init_git(&self.project_name, project_path_str.as_str())?; let auto_install = self.auto_install.unwrap_or(true); if auto_install { - install_deps(&self.npm, || self.call_success()).await?; + install_deps(&self.npm, project_path_str.as_str(), || self.call_success()).await?; } else { self.call_success(); } diff --git a/crates/taro_init/src/utils.rs b/crates/taro_init/src/utils.rs index 823c9b85ce33..0b409758c1fd 100644 --- a/crates/taro_init/src/utils.rs +++ b/crates/taro_init/src/utils.rs @@ -128,7 +128,7 @@ where } } -pub async fn install_deps(npm: &NpmType, cb: F) -> anyhow::Result<()> +pub async fn install_deps(npm: &NpmType, project_path: &str, cb: F) -> anyhow::Result<()> where F: FnOnce(), { @@ -139,6 +139,10 @@ where "执行安装项目依赖 {}, 需要一会儿...", style(command.to_owned() + " install").cyan().bold() ); + + // 确保在项目目录中执行 + env::set_current_dir(project_path)?; + let output = execute_command(command, &["install"]).await; match output { result::Result::Ok(_) => {