Run grunt build command on Travis CI - node.js

I am using Travis CI to test and build my project and as part of it I want travis to run grunt build i have tried the following but have had no luck.
script: "grunt build"
script: "./node_modules/grunt build"
script: "./node_modules/grunt/grunt build"
script: "./node_modules/grunt/grunt.js build"

Have you made sure to install grunt-cli globally on your Travis node?
My Travis CI config looks like:
language: node_js
node_js:
- "0.8"
before_install: npm install -g grunt-cli
install: npm install
before_script: grunt build
And my package.json:
{
...
"scripts": {
"test": "grunt test"
},
...
}
I will explain the flow of steps that Travis will execute:
The first step to be executed is the before_install. My only prerequisite (besides node.js) is grunt-cli so I use this step to install it.
Next is the install step, in my case this will simply install my npm modules
The before script is then executed, running grunt build
Lastly Travis will look for scripts in the package.json, there I indicated the test step should run grunt test
I'd like to note that this is my own opinion on how to configure Travis. I'm certainly not inclining you should use exactly the same approach.

You likely miss in your travis.yml file:
before_script:
- npm install -g grunt-cli
Then "grunt whatever" should execute ok (assuming you do require grunt in your devDependencies in your package.json).
(see http://www.mattgoldspink.co.uk/2013/02/10/using-travis-ci-with-grunt-0-4-x/)

Make sure that you have grunt as part of your devDependencies. Here is a sample file: https://github.com/fraxedas/raspi-cloud/blob/master/package.json
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.2",
"grunt-contrib-watch": "^0.6.1"
}
Travis-ci will install grunt on the install step:
npm install
...
grunt#0.4.5 node_modules/grunt
...
In my case I wanted to run jshint with grunt.
Here is my travis.yml file: https://github.com/fraxedas/raspi-cloud/blob/master/.travis.yml
To integrate grunt all I needed was:
before_script: grunt jshint
You can change jshint by another command.

My .travis.yml looks like this:
It runs much faster than npm as NodeJSpackage manager, I'm using Yarnon this example. It installs yarn, grunt cli, rubyand sass.
Hopefully it helps.
language: node_js
node_js:
- "7.1.0"
before_install:
- npm install -g yarn
- yarn add global ruby
- gem install sass
install:
- yarn add global sass
- yarn add global grunt-cli
- yarn add yarn install
before_script: grunt

Related

ng test --no-watch --watch=false never exiting in gitlab CI

As the title says, I simply want to run ng test from within gitlabCI.. but all the flags I've seen mentioned don't affect anything.. the routine just sits and sits. are these flags enough or do I need to setup jest.config.js ? I see mention of karma.conf.d in other tutorials but I do not have that file at all in the project
package.json
"scripts": {
"affected:apps": "nx affected:apps",
...
"test:ci": "ng test --watch=false --no-watch",
.gitlab-ci.yml
run-ng-test-ci:
tags:
- juju
stage: test
image: registry.gitlab.com/jrgemcp-public/gitlab-cicd-docker/build-mahrio-docker:latest
script:
- node --version
- npm --version
- npm ci --cache .npm --prefer-offline --ignore-scripts --quiet
- npm run test:ci
$ node --version
v14.20.0
$ npm --version
6.14.17
EDIT: Seems it's perhaps a Jest issue
https://forum.gitlab.com/t/runner-hangs-sometimes-after-all-jest-tests-are-completed/38853

sh: husky: command not found

I've setup a node project with husky but when my collegue tries to run npm install on his Mac he gets the following error :
noa-be#1.0.0 prepare
husky install
sh: husky: command not found
npm ERR! code 127
npm ERR! path /Users/X/Desktop/Workspace/project
npm ERR! command failed
npm ERR! command sh -c husky install
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/X/.npm/_logs/2021-04-12T13_07_25_842Z-debug.log
These are the relevant package.json parts:
{
"scripts": {
"prepare": "husky install"
},
"devDependencies": {
"husky": "^5.2.0",
}
}
I thought this would be enough for husky to be installed when running npm install, but it's not. What am I missing?
If you are using nvm, you might want to create a file called .huskyrc in your home directory and add the following lines of code to it:
~/.huskyrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
I was struggling with the same exact problem for hours. Finally, I could install dependencies and start working on my project by doing this:
Temporarily remove the "prepare": "husky install" script from the package.json file.
Run npm i (npm install). Dependencies installed successfuly.
Add again the "prepare" script that you removed in step 1.
Run again npm i to install the husky git hooks, so husky can do its job from now on.
This error is also thrown by npm ci if the NODE_ENV is set to "production" pre-install
I've been able to solve the problem by upgrading to latest Husky version (7.0.1, from 5.2.0).
Git was also helpful, and told me that the files weren't executables. (Git V 2.24.1)
So I give them executable rights :
chmod +x PATH_TO_HUSKY_FILE
You'll need to execute this command for every hooks
I believe it could be version specific issue. Install version 6, npm i husky#6.0.0 --save-dev, and it should work as the husky doc says.
Apparently, when I did npm i husky --save-dev, it was installing "husky": "^0.8.1" for me for some strange reason, giving me the exact same error: sh: husky: command not found.
Method 1:
Update manually, in your package.json:
{
"scripts": {
"prepare": "husky install",
"create-hook": "husky add .husky/pre-commit \"npm test\"",
}
}
Then, run npm run prepare && npm run create-hook.
It should create .husky directory with .pre-commit file in it.
Method 2:
npx husky install
npm set-script prepare "husky install"
npx husky add .husky/pre-commit "npm test"
It worked in my terminal but not in VSCode version control. So had to force quite the vscode app and restarting it worked.
Faced this issue in Github Desktop.
solved it by quit Github Desktop and re-open it.
I was able to fix this by providing an explicit location for husky
"scripts": {
"prepare": "node_modules/.bin/husky-run install"
},
Using Lerna
When I upgraded husky from version 4 to 8 there was information todo first pre commit manually. For this purpose pre-commit bash script was generated in .husky directory.
What I had todo was simply run the command included in this file:
lerna run precommit --concurrency 2 --stream

npm install everything except named package

An extract from my Dockerfile which builds my Angular app:
# Do other stuff
RUN npm install
# next line executes ng build --prod
RUN npm run build:prod
The npm install is for build purposes. Some of the packages in devDependencies - particularly cypress - take ages to install and are not needed for the build. However, some packages in devDependencies are needed.
Can I, for example, do an npm install everything except cypress?
npm install --only=prod is not an option because some devDependencies are needed.
Thanks in advance for your thoughts.
Just a suggestion. Use install-subset, and can be installed globally with npm install -g install-subset
To use it, you build inclusion lists and exclusion lists for named installation subsets in your package.json like this:
"subsets": {
"build": {
"include": [
"babel-cli",
"dotenv"
]
},
"test": {
"exclude": [
"eslint",
"lint-rules",
"prettier"
]
}
}
Then run install-subset test
This will temporarily rewrite your package.json to not install those packages excluded, then restore it (very similar to how lerna operates), which depending on the packages can save a lot of time and bandwidth.

package.json scripts that work with npm and yarn?

I am using npm as a build tool and so in my package.json, and some of my scripts depend on other scripts:
{
"test": "npm run lint && mocha"
}
This hardcodes the npm package manager into package.json. How can make this approach to expressing dependencies work with both npm and yarn?
The $npm_execpath environment variable refers to the build tool, so just replace npm with the $npm_execpath:
{
"test": "$npm_execpath run lint && mocha"
}
Both npm test and yarn test will work, and will use the appropriate build tool.
While mjs' answer is great, there's also a small package that is purported to work on all environments including Windows: https://www.npmjs.com/package/yarpm
To use in a project, run yarn add yarpm --dev / npm i -D yarpm and then just use yarpm in your scripts like this:
{
"test": "yarpm run lint && mocha"
}
As the package README notes, you just need to make sure your commands would be suitable for passing through to either yarn or npm: you cannot use arguments/flags that only work on one package manager.

How can I invoke npm on heroku command line (to install bower components)?

Bower is for client side Javascript what npm is for the server side and reads a component.json file to recognize dependencies that should be fetched at deploy time so I'd be happy it heroku would run it at slug compilation time.
Unfortunately I can not invoke npm or bower from a heroku console or one-off command (heroku run "npm help") (heroku run bash -> npm help) as it's possible with ruby's rake. I've put npm and node (latest/x versions) in my package.json but in the engines section, not the dependencies.
I think this could be solved by customizing the node buildpack but I consider this a little too heavy task just for activating something so obvious.
You can also setup a postintall command, something like this in your package.json
"dependencies": {
"bower": "0.6.x"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install"
}
Then npm install will also install bower dependencies.
Pros : one command to rule them all.
Cons : you unnecessarily embed bower as a dependency.
You can use run like this:
heroku run npm install git://github.com/webjay/kaiseki
You should declare NPM dependencies in the package.json file
All you install from shell will be deleted on exit shell. You are in a cloned instance.
You can use bower directly like this
"dependencies": {
"bower": "^1.7.9"
},
"scripts": {
"postinstall": "sudo bower install --allow-root "
}

Resources