每次从实际的应用程序项目文件夹运行../my_dir/my_script.js时,我都想运行node index.js。
换句话说,我需要my_script.js在每个应用程序(A,B,C.N)中都是通用的,并且在index.js之前执行
结构:
+my_dir
-my_script.js
+appA
+node_modules
-package.json
-index.js
+appB
+node_modules
-package.json
-index.jsmy_script.js
console.log('my_script.js from parent directory STARTED');package.json (类似于每个appA,appB .等)
{
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
//I HAVE TRIED
"start": "node ../my_dir/my_script.js",
"postinstall": "npm run-script ../my_dir/my_script.js",
"preinstall": "npm run --prefix ../my_dir/my_script.js",
}
}index.js (类似于每个appA,appB .等等);
console.log('index.js STARTED');如果我在appA node index.js里面尝试,我得到了index.js STARTED
如果我在appA npm start里面尝试,我得到了my_script.js from parent directory STARTED
预期:(同时运行):
my_script.js from parent directory STARTED
index.js STARTED知道怎么做到吗?
发布于 2022-11-30 14:24:52
我认为在使用命令行node命令时,不可能自动运行第二个脚本。您可以手动运行这两个脚本。
node ../my_dir/my_script.js && node index.js或者将它们捆绑在您的package.json中
"start": "node ../my_dir/my_script.js && node index.js",然后用
npm start您的postinstall和preinstall不太正确,它们在执行npm install时运行(您可以将post和pre关键字放在任何命令https://docs.npmjs.com/cli/v9/using-npm/scripts之前)
所以你也可以
"start": "node index.js",
"prestart": "node ../my_dir/my_script.js"然后用
npm start再一次
https://stackoverflow.com/questions/74629155
复制相似问题