How to run npm <script> delegate a child package package.json?

I have 2 levels of package.json files.

Example:

https://github.com/justin808/react-webpack-rails-tutorial

The reason is that the top level is a Rails application, and I put all the node tools in a directory called client, with my own package.json file. The top level of package.json is convenience, as well as the hook for node buildpack to run the npm install script.

I have an example of forwarding the gulp . Is there any way to generally move everything that isn't found from the top level of package.json to the child?

Top level 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" } 

Subdirectory 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" } } 
+8
source share
2 answers

You can use npm run scripts to simplify the transaction (see npm-scripts ). In parent package.json :

  "scripts": { ... "client-build": "cd client && npm run build" } 

If the client has package.json with the npm run build to generate client-side code.

Then call npm run client-build as part of the shell command of other tasks. For example:

  "scripts": { "start": "npm run client-build && gulp some-task", ... } 

This can help split the child project into a separate module with its own git repository and create it through a postinstall script. In this case, when you run npm install in the parent project, the child will have the opportunity to independently build.

+7
source share

you can write a batch file into which you enter the gulp command. Then you should check the error status. It might look like this:

 @echo off :RUN_GULP echo Running Gulp... gulp goto END :END if %ERRORLEVEL% neq 0 goto PROCESS_ERROR exit :PROCESS_ERROR cd ./client gulp exit; 

Then you just need to call the script in your package.json as follows:

 "gulp": "call ./path/to/batfile.bat" 

Did the same in my project ....

EDIT: for all scripts ... you can create one batch file that takes a script name as a parameter. The script does the same as above, but it should work for each command.

NOTE. You should use something like start path/to/batchfile.bat gulp instead of npm run gulp . Error handling does not work for npm errors!

It might look like this:

 @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 
0
source share

All Articles