NodeJS 如何同时为不同的项目运行npm install

uxhixvfz  于 2023-06-29  发布在  Node.js
关注(0)|答案(2)|浏览(248)

我正在创建的构建实际上由3个不同的项目组成。对于其中的两个,我需要做npm安装,对于第三个,我需要在打包和创建构建之前做bower安装。所以我在这里寻找优化机会并创建了这个脚本:

install_dependency.sh

# $1 = project_1 $2=project_2 $3=project_3

npm install --global bower ng-cli &&              <!-- This runs 1st -->
echo "Global Installation complete" &&            <!-- This runs 2nd -->
node --version && npm --version &&                <!-- This runs 3rd -->
parallel --halt 2 ::: \                           <!-- From here till END runs in parallel -->
"cd $1; npm install" \
"cd $1; npm install --only=dev" \
"cd $2; npm install" \
"cd $2; npm install --only=dev" \                 <!-- "END" Till here it runs parallel-->
"cd $3; bower --allow-root install" && echo All is OK &&    <!-- It runs next -->
cd $1; npm run build_stage && echo build created && <!-- It runs next --> 
cd $2; npm run build  && echo build created    <!-- It runs next -->

但似乎我不能并行运行npm install,因为它会导致一些冲突,并会出现一些随机错误,比如:

> node-sass@4.9.4 install /data/project/uiv2/node_modules/node-sass
> node scripts/install.js

npm WARN deprecated typings@1.5.0: Typings is deprecated in favor of NPM @types -- see README for more information
npm WARN deprecated browserslist@2.11.3: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN deprecated gulp-util@3.0.7: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated hoek@2.16.3: The major version is no longer supported. Please update to 4.x or newer
module.js:550
    throw err;
    ^

Error: Cannot find module 'inherits'
    at Function.Module._resolveFilename (module.js:548:15)

如果可能的话,请帮我解决这个问题。如果不是,那么解决这个问题的最佳方法是什么。

qaxu7uf2

qaxu7uf21#

并行构建的问题可能可以被dockerized。例如,在$1中构建的一个命令:

docker run -it --rm -v $(pwd)/$1:/srv -w=/srv node:8.11.3 "npm install && npm install --only-dev && npm run build_stage && echo build created"

你甚至不需要改变目录。这将做的是:

  • docker run
  • 交互式--it
  • 进程完成后的清理---rm
  • 挂载项目目录到容器目录/srv--v $(pwd)/$1:/srv
  • 设置工作目录--w=/srv
  • 使用图像-node:8.11.3
  • 命令为-npm install --global bower ng-cli && npm install && npm install --only-dev && npm run build_stage && echo build created

完成后,$1下的项目将像在主机上构建一样构建。
然后你将有三个这样的命令(根据你的脚本),你可以并行运行它们。要说明脚本,请执行以下操作:

parallel --halt 2 ::: \ 
  "docker run -it --rm -v $(pwd)/$1:/srv -w=/srv \
    node:8.11.3 sh -c 'npm install --global bower ng-cli && npm install && npm install --only-dev && npm run build_stage && echo $1 build created'"
  "docker run -it --rm -v $(pwd)/$2:/srv -w=/srv \
    node:8.11.3 sh -c 'npm install --global bower ng-cli && npm install && npm install --only-dev && npm run build && echo $2 build created'"
  "docker run -it --rm -v $(pwd)/$3:/srv -w=/srv \
    node:8.11.3 sh -c 'npm install --global bower ng-cli && bower --allow-root install'"

请注意,本例中的命令不是出于说明目的的精确猴子键入,并且未进行测试。

daolsyd0

daolsyd02#

可以使用turbohttps://turbo.build/repo/docs/installing
示例:

package.json

{
  "name": "example",
  "private": true,
  "scripts": {   
-    "test": "yarn workspace project1 test && yarn workspace project2 test"
+    "test": "turbo test",
  },
  "workspaces": {
    "packages": [
      "projects/project1",
      "projects/project2"
    ]
  },
  "devDependencies": {
+     "turbo": "^1.10.6"
  }
}

相关问题