How to have npm run <script> delegate to child package.json? - node.js

I've got 2 levels of package.json files.
Example is here:
https://github.com/justin808/react-webpack-rails-tutorial
The reason is that the top level is a Rails App, and I'm putting all node tools under a directory called client, with it's own package.json file. The top level package.json file is a convenience as well as a hook for the node buildpack to run the npm install script.
I've got an example of forwarding the gulp command. Any way to generically forward anything not found from the top level package.json to the child one?
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"
}
}

You can use npm run scripts to simplify the transaction (see npm-scripts). In the parent package.json:
"scripts": {
...
"client-build": "cd client && npm run build"
}
Where the client has a package.json with the npm run build command for building the client-side code.
Then invoke npm run client-build as part of the shell command of other tasks. For instance:
"scripts": {
"start": "npm run client-build && gulp some-task",
...
}
It may help to break the child project out into a separate module with its own git repo and building it through a postinstall script. In that case, when running npm install on the parent project, the child will have a chance to build itself.

you could write a batch file where you put the gulp-command. Then you have to check the errorstate. That could 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 have to call the script in your package.json like this:
"gulp": "call ./path/to/batfile.bat"
Did the same on my project....
EDIT: For all scripts.... you could create one batchfile that takes the script name as parameter. the script does the same like above, but it should work for every command.
NOTE: You have to use something like start path/to/batchfile.bat gulp instead of npm run gulp. Errorhandling do not work for npm errors!
This could 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

Related

npm start and other scripts do nothing [duplicate]

This question already has answers here:
NPM run * doesn't do anything
(2 answers)
Closed 3 years ago.
Until starting up my computer this morning the following worked:
npm run start
which would run my custom script in package.json and essentially boot up electron with react.
However now running the same command does absolutely nothing. The terminal just returns.
~/myapp > npm run start
~/myapp >
I have tried the following with and without step 3 and it didn't work.
Delete node_modules
Delete package-lock.json
Run npm cache clean -f
Run npm i
Try again.
I have uninstalled and reinstalled node.
My setup is:
~/myapp > node -v
v13.5.0
~/myapp > npm -v
6.13.4
and my package.json is as follows:
{
"name": "...",
"version": "...",
"description": "...",
"author": "...",
"build": {
"appId": "appname.app",
"mac": {
"icon": "build/icon.png"
},
"extraResources": [
"./public/**"
]
},
"homepage": "./",
"main": "public/electron.js",
"dependencies": {
"#babel/runtime": "^7.7.7",
"#material-ui/core": "^4.8.2",
"#material-ui/icons": "^4.5.1",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"array-move": "^2.2.1",
"cross-env": "^6.0.3",
"electron-context-menu": "^0.15.2",
"electron-is-dev": "^1.1.0",
"electron-window-state": "^5.0.3",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-dropzone": "^10.2.1",
"react-pdf": "^4.1.0",
"react-scripts": "3.3.0",
"react-smooth-dnd": "^0.11.1",
"typeface-roboto": "0.0.75"
},
"scripts": {
"react-start": "GENERATE_SOURCEMAP=false react-scripts start ",
"react-build": "GENERATE_SOURCEMAP=false react-scripts build",
"react-test": "GENERATE_SOURCEMAP=false react-scripts test",
"react-eject": "GENERATE_SOURCEMAP=false react-scripts eject",
"electron-build": "electron-builder",
"build": "npm run react-build && npm run electron-build",
"start": "concurrently \"cross-env BROWSER=none npm run react-start\" \"wait-on http://localhost:3000 && electron .\""
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"concurrently": "^5.0.2",
"electron": "^7.1.7",
"electron-builder": "^21.2.0",
"wait-on": "^3.3.0"
}
}
Note: I had to add GENERATE_SOURCEMAP=false to the scripts to fix a bug in a dependency.
Can anyone please help?
EDIT:
I just created a completely new project to test the problem as follows:
npm init react-app testapp
cd testapp
npm start
and the problem persists.
Probably ignore-scripts=true was set in .npmrc
Run: npm config set ignore-scripts false

npm run scripts does not working

I have just initialized a new project with Node.js and trying making the scripts in package.json file to be working.
For example I have the next package.json file:
{
"name": "my-app-name",
"version": "1.0.0",
"description": "Description server",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"author": "Something LTD",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"cron": "^1.3.0",
"crypto": "^1.0.1",
"docusaurus-init": "^1.0.2",
"exceljs": "^1.5.0",
"express": "^4.16.3",
"express-limiter": "^1.6.1",
"jsonwebtoken": "^8.3.0",
"mongojs": "^2.6.0",
"npm": "^6.1.0",
"point-in-polygon": "^1.0.1",
"request": "^2.87.0",
"socket.io": "^2.1.1",
"socket.io-redis": "^5.2.0",
"xmldom": "^0.1.27"
},
"devDependencies": {
"t4u": "^1.0.0"
}
}
Then I either trying run npm test or npm start or npm run start
but all of them just doing nothing and returns nothing in console. Even the test script just not printing anything.
I have tried to do:
npm config set --ignore-scripts false
However that did not work.
npm config set ignore-scripts false
Was solved the issue.
npm run-script start worked for me (npm run is an alias to npm run-script as stated in doc but not sure why alias didnt work)
hope this helps people who are still facing the issue after setting ignore-script as false
On Windows you can also edit directly a file on the following location
C:\Users\[Your username]\.npmrc
and set ignore-scripts=false

Why does NodeJS require me to use the full directory for downloaded modules?

For something like Express for example, which does not come with Node by default. I have to use var express = require('C:/Users/User/node_modules/express'); instead of just var express = require('express');. I notice the modules which come by default such as http aren't in the same location as the ones I install. So what do I need to do in order to not have to write the whole directory. If it makes any difference I keep all my Node projects in C:/Node/, not the default one.
This is happening because you probably don't have node modules installed locally. For that you need a package.json file which you can get by running
npm init
This will ask you some questions about your project and will set up node locally. A package.json file will be created which should look something like this (without dependencies).
{
"name": "express-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "Link to your repository"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^1.0.2",
"body-parser": "^1.17.2",
"chalk": "^2.0.1",
"compression": "^1.7.0",
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"express": "^4.15.3",
"glob": "^7.1.2",
"moment": "^2.18.1",
"mongoose": "^4.11.3",
"morgan": "^1.8.2",
"passport": "^0.3.2",
"path": "^0.12.7",
"yargs": "^8.0.2"
}
}
You can then add the node modules you want by putting them in dependencies and running
npm install
If you want to add node modules from commond line you can use
npm install package-name --save

Angular2 : concurrent is not recognized as internal or external command

While running server for angular2 using npm start is giving error that concurrent is not recognized as internal or external command, why is it happens these kind of error
here is package.json:
{
"name": "contactlistapp",
"version": "1.0.0",
"description": "The app",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"tsc": "./node_modules/.bin/tsc",
"tsc:w": "./node_modules/.bin/tsc -w",
"serve": "./node_modules/.bin/live-server --host=localhost --port=3000 .",
"start": "concurrent \"npm run tsc:w\" \"npm run serve\" "
},
"author": "Sarah",
"license": "ISC",
"dependencies": {
"express": "^4.13.3",
"angular2": "2.0.0-beta.0",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
"zone.js": "0.5.10"
},
"devDependencies": {
"concurrently": "^1.0.0",
"lite-server": "^1.3.1",
"typescript": "^1.7.3"
}
}
structure of the project is there is node_module and typings folder there is app.ts file for server(empty only express is imported), and component.ts(and their .js), tsconfig.json
concurrent is not recognized as internal or external command
Must likely you haven't ran npm install since you added the "concurrently": "^1.0.0" dependency.
live-server is not recognized as external or internal command
You are installing the lite-server :
"lite-server": "^1.3.1"
yet you are calling the live-server:
"serve": "./node_modules/.bin/live-server --host=localhost --port=3000 ."
call lite-server instead.

ubuntu npm install not installing dependencies or dev dependencies

I have cloned a project's repo (that was made on a mac) down to my ubuntu machine and I hit a lot of problems getting node to work. I feel like I'm at the final hurdle.
I can't get npm install to install dependencies according to the package.json file.
Note in the following, I have used <repo description>, <repo name> and <app name>, where appropriate, for privacy.
package.json (git repo name removed for privacy):
{
"name": "<repo name>",
"version": "1.0.0",
"engines": {
"npm": "2.1.x"
},
"description": "<repo description>",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "bower install",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/<repo name>"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/<repo name>/issues"
},
"homepage": "https://github.com/<repo name>#readme",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-sass": "^0.9.2",
"grunt-contrib-watch": "^0.6.1",
"jasmine-core": "^2.3.4",
"karma": "^0.13.9",
"karma-chrome-launcher": "^0.2.0",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^0.2.1",
"phantomjs": "^1.9.18",
"protractor": "^2.2.0"
},
"dependencies": {
"bower": "^1.5.2",
"ejs": "^2.3.4",
"express": "^4.13.3",
"http-server": "^0.8.0"
},
"directories": {
"test": "test"
}
}
When running npm install, I get:
npm WARN package.json <app name>#1.0.0 No README data
> <app name>#1.0.0 postinstall /home/andrew/projects/<app name>
> bower install
andrew:<app name>$
When running sudo npm install, I get:
npm WARN package.json <app name>#1.0.0 No README data
npm WARN cannot run in wd <app name>#1.0.0 bower install (wd=/home/andrew/projects/<app name>)
node -v returns v0.12.7, nodejs -v returns v0.10.25, npm -v returns 2.12.1, bower -v returns 1.5.2, but grunt -v returns:
No command 'grunt' found, did you mean:
Command 'grun' from package 'grun' (universe)
grunt: command not found
(just proof that grunt has not been installed as it should).

Resources