Node.js 如何运行npm<;脚本>;委托给child package.json?
我有两个级别的Node.js 如何运行npm<;脚本>;委托给child package.json?,node.js,npm,package.json,Node.js,Npm,Package.json,我有两个级别的package.json文件 例如: 原因是顶层是一个Rails应用程序,我将所有节点工具放在一个名为client的目录下,它有自己的package.json文件。顶层的package.json文件是一个方便的工具,也是节点buildpack运行npm安装脚本的钩子 我有一个转发gulp命令的示例。有没有办法将顶级package.json中未找到的内容一般转发给子级 顶级package.json { "name": "react-webpack-rails-tutorial"
package.json
文件
例如:
原因是顶层是一个Rails应用程序,我将所有节点工具放在一个名为client的目录下,它有自己的package.json
文件。顶层的package.json
文件是一个方便的工具,也是节点buildpack运行npm安装
脚本的钩子
我有一个转发gulp
命令的示例。有没有办法将顶级package.json
中未找到的内容一般转发给子级
顶级package.json
{
"name": "react-webpack-rails-tutorial",
"version": "1.1.1",
"description": "Code from the React Webpack tutorial.",
"main": "server.js",
"engines": {
"node": "0.10.32"
},
"scripts": {
"postinstall": "cd ./client && npm install",
"gulp": "cd ./client && npm run gulp"
},
"repository": {
"type": "git",
"url": "https://github.com/justin808/react-webpack-rails-tutorial.git"
},
"keywords": [
"react",
"tutorial",
"comment",
"example"
],
"author": "justin808",
"license": "MIT",
"bugs": {
"url": "https://github.com/justin808/react-webpack-rails-tutorial/issues"
},
"homepage": "https://github.com/justin808/react-webpack-rails-tutorial"
}
子目录package.json
{
"name": "react-webpack-rails-tutorial",
"version": "1.1.0",
"description": "Code from the React Webpack tutorial.",
"main": "server.js",
"engines": {
"node": "0.10.32"
},
"repository": {
"type": "git",
"url": "https://github.com/justin808/react-webpack-rails-tutorial.git"
},
"keywords": [
"react",
"tutorial",
"comment",
"example"
],
"author": "justin808",
"license": "MIT",
"bugs": {
"url": "https://github.com/justin808/react-webpack-rails-tutorial/issues"
},
"homepage": "https://github.com/justin808/react-webpack-rails-tutorial",
"dependencies": {
"babel-core": "^5.0.8",
"babel-loader": "^5.0.0",
"body-parser": "^1.12.2",
"es5-shim": "^4.1.0",
"imports-loader": "^0.6.3",
"jquery": "^2.1.3",
"loader-utils": "^0.2.6",
"marked": "^0.3.3",
"react": "^0.13.1",
"react-bootstrap": "^0.20.1",
"sleep": "^2.0.0",
"webpack": "^1.7.3"
},
"devDependencies": {
"babel-eslint": "^2.0.2",
"bootstrap-sass": "^3.3.4",
"bootstrap-sass-loader": "^1.0.3",
"css-loader": "^0.9.1",
"eslint": "^0.18.0",
"eslint-plugin-react": "^2.0.2",
"expose-loader": "^0.6.0",
"express": "^4.12.3",
"file-loader": "^0.8.1",
"gulp": "^3.8.11",
"gulp-eslint": "^0.8.0",
"node-sass": "^2.1.1",
"react-hot-loader": "^1.2.4",
"sass-loader": "^0.6.0",
"style-loader": "^0.9.0",
"url-loader": "^0.5.5",
"webpack-dev-server": "^1.8.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"gulp": "gulp"
}
}
您可以编写一个批处理文件,在其中放置gulp
-命令。然后你必须检查错误状态。可能是这样的:
@echo off
:: Check if script is defined
set _script=%1
if "%_script%"=="" goto NO_SCRIPT_DEFINED
:START_APP
npm run %_script%
goto END
:NO_SCRIPT_DEFINED
echo ERROR: script was not defined
pause
exit
:END
if %ERRORLEVEL% neq 0 goto NO_PARENT_SCRIPT
exit
:NO_PARENT_SCRIPT
echo searching in ./client ...
cd ./client
npm run %_script%
exit
然后您只需调用package.json中的脚本,如下所示:
@echo off
:: Check if script is defined
set _script=%1
if "%_script%"=="" goto NO_SCRIPT_DEFINED
:START_APP
npm run %_script%
goto END
:NO_SCRIPT_DEFINED
echo ERROR: script was not defined
pause
exit
:END
if %ERRORLEVEL% neq 0 goto NO_PARENT_SCRIPT
exit
:NO_PARENT_SCRIPT
echo searching in ./client ...
cd ./client
npm run %_script%
exit
在我的项目上也做了同样的事
编辑:对于所有脚本。。。。您可以创建一个以脚本名称为参数的批处理文件。脚本的作用与上面相同,但它应该适用于每个命令
注意:您必须使用类似于start path/to/batchfile.bat gulp
的内容,而不是npm run gulp
。错误处理不适用于npm错误
这可能是这样的:
@echo off
:: Check if script is defined
set _script=%1
if "%_script%"=="" goto NO_SCRIPT_DEFINED
:START_APP
npm run %_script%
goto END
:NO_SCRIPT_DEFINED
echo ERROR: script was not defined
pause
exit
:END
if %ERRORLEVEL% neq 0 goto NO_PARENT_SCRIPT
exit
:NO_PARENT_SCRIPT
echo searching in ./client ...
cd ./client
npm run %_script%
exit
您可以使用
npm run
脚本简化事务(请参阅)。在父package.json
中:
"scripts": {
...
"client-build": "cd client && npm run build"
}
其中,客户端有一个package.json
,带有npm run build
命令,用于构建客户端代码
然后调用npm run client build
,作为其他任务的shell命令的一部分。例如:
"scripts": {
"start": "npm run client-build && gulp some-task",
...
}
它可能有助于将子项目分解成一个单独的模块,该模块具有自己的git repo,并通过安装后脚本进行构建。在这种情况下,在父项目上运行npm install
时,子项目将有机会自行构建