Node.js Heroku Deployment - Fails To Exec Postinstall Script To Install Bower - javascript

Deployment of my Node.js MEAN app to heroku fails with the following errors. I can't figure out what is wrong with the bower install...
Here is the error message:
2606 info postinstall App#1.0.0
2607 verbose unsafe-perm in lifecycle true
2608 info App#1.0.0 Failed to exec postinstall script
2609 error App#1.0.0 postinstall: `./node_modules/bower/bin/bower install`
2609 error Exit status 1
2610 error Failed at the App#1.0.0 postinstall script.
2610 error This is most likely a problem with the App package,
2610 error not with npm itself.
2610 error Tell the author that this fails on your system:
2610 error ./node_modules/bower/bin/bower install
! Push rejected, failed to compile Node.js app
Here is my Bower.json
{
"name": "mean",
"version": "1.0.0",
"dependencies": {
"bootstrap": "*",
"angular": "*",
"angular-resource": "*",
"angular-cookies": "*",
"angular-ui-utils": "*",
"angular-bootstrap": "*",
"json3": "*",
"jquery": "*",
"angular-ui-router": "*",
"angular-animate": "*",
"move.js": "git://github.com/visionmedia/move.js.git#~0.3.3",
"animate.css": "*",
"ngAnimate-animate.css": "*",
"angularLocalStorage": "~0.1.7",
"jquery-nicescroll": "*"
},
"resolutions": {
"angular": "1.2.4"
}
}
Here is my Package.json
"scripts": {
"start": "node node_modules/grunt-cli/bin/grunt",
"test": "node node_modules/grunt-cli/bin/grunt test",
"postinstall": "./node_modules/bower/bin/bower install"
},

I get this error a lot too. every third push to heroku fails because of bower postinstall.
While this is not a robust fix, and I don't fully understand why it helps! but this hepled me, so hopefully will help someone else.
Despite /lib folder is being added to .gitignore, force add it before deploying heroku
git add -f public/lib
git commit -m "force add bower libs"
git push heroku master

This is likely related to this issue with bower, the cause of which is currently still being investigated:
https://github.com/bower/bower/issues/933
I've also been having some similar issues with the bower install command failing on heroku. Here's what worked for me:
1. Temporarily remove node_modules and bower_components from .gitignore.
This seemed to fix an ENOENT error when trying to install Angular using bower through a postinstall script in heroku.
Note: If you specify a different installation directory for bower components in your .bowerrc file, then make sure that directory is not present in your .gitignore.
2. Edit (or create) .bowerrc and tell it to use temp directories that are local to the project directory:
{
"storage": {
"packages": ".bower-cache",
"registry": ".bower-registry"
},
"tmp": ".bower-tmp"
}
By default, bower was trying to use a directory in /app, which was resulting in ENOTEMPTY errors (maybe because it was trying to clear those directories, but it didn't have access because they are shared with other users? Just throwing out a guess...)
Using a directory that's local to the project fixed the conflicts.
Hope this helps someone else.
Note: Even after performing the above steps, the bower install command may still occasionally fail. However, it generally works the second or third time - just try running the command again... Until the underlying issue is resolved, that's the best advice that I can offer.

I had the same issue. The problem was that in the bower.json file:
{
"name": "mean",
"version": "0.1.3",
"dependencies": {
"angular": "1.2.8",
"angular-resource": "latest",
"angular-cookies": "latest",
"angular-mocks": "latest",
"angular-route": "latest",
"bootstrap": "3.0.3",
"angular-bootstrap": "0.10.0",
"angular-ui-utils": "0.1.0"
}
}
"bower install" is unable to determine the angular version and requires manual intervention to choose the right version:
Unable to find a suitable version for angular, please choose one:
1) angular#1.2.8 which resolved to 1.2.8 and has mean as dependants
2) angular#1.2.9 which resolved to 1.2.9 and has angular-cookies#1.2.9, angular-mocks#1.2.9, angular-resource#1.2.9, angular-route#1.2.9 as dependants
3) angular#>= 1.0.2 which resolved to 1.2.10-build.2176+sha.e020916 and has angular-ui-utils#0.1.0 as dependants
4) angular#>=1 which resolved to 1.2.10-build.2176+sha.e020916 and has angular-bootstrap#0.10.0 as dependants
Prefix the choice with ! to persist it to bower.json
[?] Answer:
So Heroku fails when it executes the script.
FIX
Just change the version of angular in your bower.json file:
"angular": "1.2.10",
1.2.9 will also work.

#ac360 This isn't an issue with bower at all. It's generally a warning you can get if different libraries use the same dependency however a different version. You should never add your public/lib to the repo. That defeats the purpose of what bower can be used for. Keep your repo as light as possible, and let dependencies download and resolve at build time so you can get the latest and greatest within the parameters defined in your bower.json
To resolve this issue completely for auto-deploys, bower gives us a property on the bower.json called resolutions
Simply create the following in your bower.json
"resolutions": {
"ember": "1.2.10"
}
The reason you still had problems even if you had resolutions defined was because the version you picked wasn't going to satisfy all dependencies so the question came up during the heroku install.
Alternatively, you can build locally, and when you are asked which version to choose from, if you preceed the number choice with the bang ! symbol, bower will update your bower.json for you!
See: https://github.com/bower/bower/issues/532

I got it working by ensuring to save bower in package.json using the command below. The save will install bower using npm on server before attempting to run bower install
npm install bower --save
the postinstall script in package.json
"postinstall:"bower install" worked on heroku after that.

Related

Is there a way to get npm install to install "bin" executables in the local node_modules directory (the .bin directory)?

I have a Node project, and I can npm pack it into a tarball and install it in another directory for testing. When I do that, the set of files in the "bin" clause of my package.json are correctly symlinked into the node_modules/.bin directory there. Thus I can use npx something to run the "something" script and it works as I would expect.
From the actual project directory, however, npm install doesn't do that. The scripts are there in my top-level "bin" directory of course, but npx doesn't find them. Obviously I could just run them directly, but it would be nice if I could run the same shell commands to run them in other installation directories and in the project home directory.
I don't want to install the package globally; I want to keep everything local, at least for now. So can I get npm to make those symlinks for me, or should I just bite the bullet and do it myself?
Here's my package.json:
{
"name": "#foo/plazadb",
"version": "0.1.0",
"description": "Basic database setup with upload/download from CSV",
"main": "lib/plazadb.js",
"author": "Pointy",
"license": "ISC",
"dependencies": {
"arg": "^5.0.1",
"cls-hooked": "^4.2.2",
"csv-parser": "^3.0.0",
"csv-stringify": "^6.0.5",
"neat-csv": "^7.0.0",
"pg": "^8.7.*",
"sequelize": "^6.16.*"
},
"bin": {
"conntest": "./bin/conntest.js",
"download": "./bin/download.js",
"upload": "./bin/upload.js"
}
}
The "bin" files all exist (of course, otherwise they would never work). What I'm trying (out of ignorance) is a simple npm install from the project directory.
One way to do is by
specifying a local prefix by using npm config set prefix <path> or one of these methods linked here.
generating symlinks using npm link
As a word of caution, to undo npm link executing npm unlink . may not work. Instead one may have to run npm uninstall -g ..

How to fix dependency error with React App using Jest

So I installed Jest in a new project and the app stopped running, due to the error below.
Summary: It's telling me that I've manually installed a dependency to node_modules, something I didn't do, and it's asking me to delete my entire node_modules and yarn.lock. But those steps aren't working.
Note: Removing the dependency from package.json then deleting node_modules and yarn.lock does fix the problem, but when I install jest again, it falls into the same problem.
The steps I took to install jest, here:
$ yarn add --dev react-test-renderer
$ yarn add --dev jest babel-jest #babel/preset-env #babel/preset-react react-test-renderer
The error message
$ react-scripts start
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"jest": "26.6.0"
Don't try to install it manually: your package manager does it automatically.
However, a different version of jest was detected higher up in the tree:
C:\WORK\plotting-a-chart\node_modules\jest (version: 26.6.3)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "jest" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if C:\WORK\plotting-a-chart\node_modules\jest is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls jest in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed jest.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
I tried doing all the steps, I already use yarn, I even cleared my yarn cache before doing the steps 1 to 4.
Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder;
Delete node_modules in your project folder;
Remove "jest" from dependencies and/or devDependencies in the package.json file in your project folder;
Run npm install or yarn, depending on the package manager you use.
I'm out of ideas.
// package.json
{
"name": "project",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"node-sass": "^5.0.0",
"prismjs": "^1.23.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-hook-form": "^6.15.5",
"react-icons": "^4.2.0",
"react-resizable": "^1.11.1",
"react-scripts": "4.0.3",
"react-vis": "^1.11.7"
},
"scripts": {
"dev": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"unit": "jest"
},
"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": {
"#babel/preset-env": "^7.13.12",
"#babel/preset-react": "^7.13.13",
"babel-jest": "^26.6.3",
"babel-plugin-root-import": "^6.6.0",
"jest": "^26.6.3",
"react-test-renderer": "^17.0.2"
}
}
// babel.config.js
module.exports = {
presets: ['#babel/preset-env', '#babel/preset-react'],
};
You don't have to npm install jest. The error is telling you :
Don't try to install it manually.
Here is what it says on react documentation
If you use Create React App, Jest is already included out of the box
with useful defaults.
To solve your issue you should run this :
npm uninstall --save-dev jest
npm uninstall --save jest
rm node_modules
npm i
I did solved this issue by creating a .env file in the project folder, with SKIP_PREFLIGHT_CHECK=true, and now the test runner seems to be working perfectly.
When I encountered this problem, I used the stack trace to figure out what version of jest it was looking for. I then yarn removed it and reinstalled the version it wanted. Just to be safe I would do the entire process it suggests but make sure you install the version it asks for

How to add npm dependency as peer dependency

Does npm have the option to install dependency as peer-dependency like yarn option --yarn, instead of adding it manually for example:
"peerDependencies": {
"#angular/core": "^7.0.0"
}
Update with more clarification of the question, thanks to #Broncha
The question is how to add a peer dependency to a project. That is
npm i dep adds the dependency to the "dependencies" in package.json,
npm i -D dep adds the dependency to the "devDependencies" in package.json.
How do I install a dependency that adds it to the "peerDependencies" in package.json?
As for now, there is NO WAY, you can install dependencies as peer-dependencies. You have to install then and manually move them to peerDependencies object in package.json
OLD ANSWER
The automatic install of peer dependencies was removed with npm v3, this feature is aging added in npm v7.
So update your npm to version 7 or higher will solve most of the problems.
If You need to install dependency as a peer dependency.
To install peer dependency, you actually need to manually modify your package.json file.
For example, if you want to install angular's core component library as a peer dependency,
npm i #angular/core
This will add a property in the dependencies object.
"dependencies": {
"#angular/core": "^7.0.0"
}
Move the installed package name to peerDependencies key.
"peerDependencies": {
"#angular/core": "^7.0.0"
}
Extra:
if you need two versions of the same package then you modify the packge.json file like this,
"peerDependencies": {
"#angular/core": "^6.0.0"
"#angular/core": "^7.0.0"
}
All the other answers are talking about How NPM command can handle installing the 'peerDeps' of the current 'deps' and 'devDeps' in package.json of current project, installing them automatically.
But the question is ask how to use NPM command with specific flag to install a deps as 'peerDeps' and write into the package.json of current project.
The ANSWER is, unfortunately, there is no such flag even till NPM#7
I guess NPM doesn't treat that a command to install deps, since adding a 'peerDeps' to package.json doesn't really need NPM to install a package to /node_modules/. It is just a file configuration change to package.json. But I understand people don't want to manually add/remove 'deps' in package.json file and want NPM to do that, it may because NPM will handle the order of the 'deps'.
Another reason is, 'peerDeps' always use a range of semver, and that has to be edit manually not via a npm install command. like react-redux:
"peerDependencies": {
"react": "^16.8.3 || ^17"
},
I think NPM#7 should provide a way to support that, since now it is officially able to process the 'peerDeps' and this feature is part of it.
you can not do it directly in npm 3 so check below reference for detials https://stackoverflow.com/a/35207983/10309265 You can do it by either way reference: https://stackoverflow.com/a/35690137/10309265

How to have yarn not issue a warning for the license field?

On running yarn install I see a warning every time that there is no license filed even though I have defined one:
$ jq . package.json
{
"name": "license-example",
"version": "1.0.0",
"main": "index.js",
"license": "UNLICENSED",
"dependencies": {
"lodash": "^4.17.4",
"moment": "^2.18.1"
}
}
which according to the npm defintion should be valid:
Finally, if you do not wish to grant others the right to use a private
or unpublished package under any terms:
{ "license": "UNLICENSED" }
Here's the output:
yarn install
yarn install v0.27.5
warning ../package.json: No license field
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.09s.
My main goal is for that warning to disappear, yet I also don't want to provide an invalid open-source LICENSE to make the warning go away, even if it is an internal project that never will be seen on the outside.
How to mark a yarn project as proprietary without a warning appearing?
For yarn and npm, the default behavior is that they look up into the parent directories.
I had an outdated and forgotten package.json in my home folder without a license field:
~/package.json
When running yarn install within my project:
~/my-project/package.json
yarn then also found the one in my home directory and reported the error for that one. I mistook that for my project's package.json.
The warning makes that clear by preceding the path with .. for the parent folder.
warning ../package.json: No license field
After removing that outdated package.json I get the expected output:
yarn install v0.27.5
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.88s.
Take a closer look at the message:
warning ../package.json: No license field
It's referring to a package.json one directory level higher.
Fix that one by either entering a license field or a private: true or delete it because it probably should not be there anyway ;-)
I was getting the following warning along with some other licensing warnings.
warning package.json: No license field
warning react-material-dashboard#0.3.0: No license field
All I did was, update the package.json file's private property to be true.
{
"name": "some-application-name",
"author": "Keet Sugathadasa",
"email": "email",
"license": "MIT",
"version": "0.0.1",
"private": true,
...
}
With this, I no longer got any No license field warnings when I do yarn install. To understand why, please see this question.
{"private": true} means, that npm will refuse to publish it, to prevent accidental publication of private repositories.
For more on this, see the following links.
https://docs.npmjs.com/files/package.json#private
https://flaviocopes.com/package-json/#private
After trying multiple solutions, i found there were following files in root, need to delete:
cd ~
~$ rm -rf package.json
~$ rm -rf yarn.lock
~$ rm -rf package-lock.json
I got stuck in the same error and I found that when we add package.json or yarn, some files can be there in the system roots. So, the errors are from there the system root. You can simply remove those files and the error will not be there anymore.
just cd ~, then you can find package.json & yarn.lock.
rm -rf package.json or rm -rf yarn.lock
I am new to the react, but I find, the most simplest way is: just add the "private": true, to your package.json file. That's it.
Just make sure you are in the directory that contains the package.json file, then just yarn or npm install then serve it as you please.
I am currently running a project without the license field and it works perfectly,
I don't think that can return an error.
Also, see more information regarding the mandatory fields you need for your package to run and other tips regarding the package.json file with yarn/npm:
https://classic.yarnpkg.com/en/docs/package-json/
https://docs.npmjs.com/files/package.json
I had similar issue, i just upgraded the version of Node and every thing worked fine.....

GRUNT - Install Grunt Packages?

Can anyone advise what I'm doing wrong here (i'm new to grunt) a colleage has supplied me with a base gruntfile setup
I have installed node,js, and grunt but I cant figure out how to install the various packages (uglify, concat etc) - which command should I use for this?
The first question you must ask yourself is, "what are the names of the grunt dependencies?"
The quick answer:
Simply execute the npm install <package-name> for each package you wish to install. For the most part, I consider the package-names to be equivalent to their github name. ( grunt-contrib-concat for example. )
npm install grunt-contrib-concat will install it, then go on to the next.
Improving Maintainability:
For optimal maintainability and cross developer/environment support you should consider using package.json to list your dependencies.
Once you have all of your dependencies listed in package.json, you can then run npm install and all of them will be installed should the need ever arise. (For development based dependency vs runtime/production based dependency management, read on!)
Best Practices:
The optimal workflow with node js is to use npm init first, so that your package.json file is created for you. then once you have a package.json, you can install each of your dependencies/devDependencies by either adding them to their respective lists in the json file or adding them to their respective lists on the fly by using npm install <package name> --save or --save dev respectivly.
NOTE: IF you are experimenting, and you'd rather NOT save a node module to your dependency list, then a simple npm install <package name> will do.
At this point, I do recommend searching for npm package.json best practices - as you will find some valuable information regarding your workflow for nodejs. Dependency management can be amazing, but only if you know how to really take advantage of the utilities provided.
Consider the following excerpt from a package.json file:
"dependencies": {
"backbone": "*",
"underscore": "~*",
"requirejs": "*"
"jquery": "*"
},
"devDependencies": {
"grunt": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-concat": "*",
"grunt-contrib-copy": "*"
},
The example we see is that the npm packages under dependencies will install when running npm install --production, the development packages will be installed automatically when running npm install --development,
NOTE: You can change your node environment as well which modifies the usage of a simple npm install with no flags.
For the most part, grunt is used for development, so I believe it would be more common to find anything related to grunt being in devDependencies
A sample package.json:
http://browsenpm.org/package.json
if you have package.json with devDependencies (or dependencies) defined, all you need to run is npm install then grunt
Otherwise run something like this to install and save them to your package.json
npm install --save-dev <package-name>

Categories