NPM doesn't install module dependencies when deploying a Grunt app to heroku - json

I'v made a static single page site using grunt. I'm now trying to deploy it to heroku using the heroku-buildpack-nodejs-grunt for node grunt.
Below is a pic of my root directory:
Here's my Gruntfile package.json:
Procfile:
web: node index.html
When I run $ git push heroku master it gets to the Gruntfile and fails:
-----> Found Gruntfile, running grunt heroku:production task
>> Local Npm module "grunt-contrib-uglify" not found. Is it installed?
The above errors proceed to list all local NPM modules as not found. If I list all loadNpmTasks instead of using "load-grunt-tasks", I get the exact same error.
When I $ heroku logs I get:
Starting process with command `node web.js`
Error: Cannot find module '/app/web.js'
Can anyone see where I've gone wrong?

For anyone passing by here, I wasn't able to solve the problem. This is where I got to:
In my Gruntfile, I moved npm modules from devDependencies to dependencies. Heroku was then able to install these dependencies.
However, when Heroku ran the tasks, it stops at the haml task w/ error "You need to have Ruby and Haml installed and in your PATH for this task to work". Adding ruby & haml to the Gruntfile as engines did not work.

The only thing I can think of is that maybe Heroku installs your devDependencies first, tries to run Grunt, but since it didn't install load-grunt-tasks yet, you don't get the grunt.loadNpmTasks( 'grunt-contrib-uglify' ); line (which load-grunt-tasks does for you), and thus Grunt can't find the package.
Can you try changing your Gruntfile to explicitly list out all npm modules using the grunt.loadNpmTasks() method?
EDIT:
Just remembered another thing I had to do:
heroku labs:enable user-env-compile -a myapp
heroku config:set NODE_ENV=production
(Obviously replacing myapp with your Heroku app name.)
This makes Heroku allow user set environment variables and then sets your server to production. Try that, and set your dependencies and devDependencies as you had them originally (just to see if it works).

I am coming pretty late to the game here but I have used a couple methods and thought I would share.
Option 1: Get Heroku to Build
This is not my favorite method because it can take a long time but here it is anyway.
Heroku runs npm install --production when it receives your pushed changes. This only installs the production dependencies.
You don't have to change your environment variables to install your dev dependencies. npm install has a --dev switch to allow you to do that.
npm install --dev
Heroku provides an article on how you can customize your build. Essentially, you can run the above command as a postinstall script in your package.json.
"scripts": {
"start": "node index.js",
"postinstall": "npm install --dev && grunt build"
}
I think this is cleaner than putting dev dependencies in my production section or changing the environment variables back and forth to get my dependencies to build.
Also, I don't use a Procfile. Heroku can run your application by calling npm start (at least it can now almost two years after the OP). So as long as you provide that script (as seen above) Heroku should be able to start your app.
As far as your ruby dependency, I haven't attempted to install a ruby gem in my node apps on Heroku but this SO answer suggests that you use multi buildpack.
Option 2: Deploy Your Dependencies
Some argue that having Heroku build your application is bad form. They suggest that you should push up all of your dependencies. If you are like me and hate the idea of checking in your node_modules directory then you could create a new branch where you force add the node_modules directory and then deploy that branch. In git this looks like:
git checkout -b deploy
git add -f node_modules/
git commit -m "heroku deploy"
git push heroku --force deploy:master
git checkout master
git branch -D deploy
You could obviously make this into a script so that you don't have to type that every time.
Option 3: Do It All Yourself
This is my new favorite way to deploy. Heroku has added support for slug deploys. The previous link is a good read and I highly recommend it. I do this in my automated build from Travis-CI. I have some custom scripts to tar my app and push the slug to Heroku and its fast.

I faced a similar problem with Heroku not installing all of my dependencies, while I had no issue locally. I fixed it by running
heroku config:set USE_NPM_INSTALL=true
into the path, where I deployed my project from. This instructs Heroku to install your dependencies using npm install instead of npm ci, which is the default! From Heroku dev center:
"Heroku uses the lockfiles, either the package-lock.json or yarn.lock, to install the expected dependency tree, so be sure to check those files into git to ensure the same dependency versions across environments. If you are using npm, Heroku will use npm ci to set up the build environment."

Related

How to "npm install" only specific dependencies automatically from package.json. (Ex. based on CLI command you only install server side dependencies)

Trying to find a way to split the dependencies installation into groups in your root's package.json file.
So, instead of just running "npm install" when cloning code and installing all dependencies. Say instead you ONLY want to install specific/designated dependencies in your package.json so you have the option of only installing a piece/group of the entire dependencies list. Something similar to running a script, but just for specifying what dependencies you want npm to install. So, could you make a script similar to "npm run start" or "npm run dev" but each script only installs specific dependencies from the package.json file.
This way you have the option of having all of your dependencies in one file location, but you also have the option of installing all of them, or only installing some of them without having to type out each dependencies name in the CLI that is in the package.json list that you want to specifically install.

'react-scripts' is not recognized as an internal or external command, operable program or batch file. json file deleted

the errror is 'react-scripts' is not recognized as an internal or external command,
operable program or batch file.
ive tried
npm install
npm install react-scripts --save
npm i -g react-scripts
https://github.com/mareyam/Complete-Maryam-s-Restaurant
i uploaded this code on github without .json file
now im trying to donwnload and use it but cant because ,json file ive lost. ive tried using json file from anothr project but not working
I would recommend using create-react-app to build this project. Run the command, cd into the directory then start the app.
npx create-react-app name_of_app
cd name_of_app
npm start
This will be good enough for development, and you can add all your components into the src/ folder. Here's the documentation for how to start building react web apps.
To get the production build (which I think is what you're trying to go for), run npm run build which will save the build in the build/ folder.
Any time you use a different machine, just pull the project from github and run npm i. I'm assuming that you were talking about the package.json file. This shouldn't be a big deal if you set everything up correctly.

Error HH12: Trying to use a non-local installation of Hardhat, which is not supported. Please install Hardhat locally using npm or Yarn, and try again

I've been working on a Hardhat project for sometime.
After a while, when I run
npx hardhat node
to start the HH server, I get the error below:
I tried researching the error and found this on the hardhat site
HH12: Hardhat is not installed or installed globally
You tried to run Hardhat from a global installation or not installing it at all. This is not supported.
Please install Hardhat locally using npm or Yarn, and try again.
Funny enough, I created a new HH project and installed everything afresh then imported my code into the new project and this seemed to solve the problem... but after sometime, the issue began again. I can't keep creating a new project each time this happens.
I've been stuck for days now and will appreciate any help.
OS: windows10
Do not install Hardhat globally. If you already have installed hardhat globally, please uninstall as the issue might be because of that as mentioned in the error message.
Things you need to do to mitigate this:
Check if the project package.json has hardhat as its dev dependency. If it is not, run npm install --save-dev hardhat or yarn add --dev hardhat
Uninstall any global version of hardhat
Remove node_modules and run npm install or yarn install to install all dependencies.
Try running npx hardhat compile or npx hardhat node to check if it works.
Try to not run hardhat using npx hardhat ..., run it locally using npm hardhat ... or yarn hardhat ..., other thing that can cause this error on windows is the git bash, if you are using the git bash you won't be able to run hardhat in that case try what running it locally with npm or yarn in the cmd
I had the same issue. What I did was run the following commands:
npm install --save-dev "hardhat#^2.10.1
npm i #nomicfoundation/hardhat-toolbox
Don't make directories/folder structure manually, instead do it by using mkdir command from vscode terminal. This way you will not face this HH12 error.
I recently created several DAPPs and I had never faced this problem again.
mkdir Whitelist-Dapp
cd Whitelist-Dapp
mkdir hardhat-files
cd hardhat-files
npm init --yes
npm install --save-dev hardhat
First thing to check when this error comes up is whether you are running
npx run scripts/deploy.js --network localhost
in the correct path or not. It should run from directory where you have put hardhat.config.js file.
To fix this problem, go into your terminal and write nvm use 17. It will switch the node back to 17. The problem is that you may have inadvertently updated your hardhat version so you have 2 versions running on your machine.
You can also check whether traces tell you something (npx hardhat --show-stack-traces). I tried reinstalling nvm, node, searched whole PC for anything that has hardhat within file name and deleted it.
In the end I was missing some packages in my project...
In my case I had a problem with the package-lock.json
I deleted the package-lock.json file and then ran the command
npm install
That solved the problem for me.
I found a way to avoid this issue by typing the commands in the following sequence. I think it has something to do with when you install your Hardhat in the folder.
I am creating my project in the directory called JACKPOT, and then I type the commands in the following order. So far so good.
Frist create your project folder and then switch to it:
PS C:\Users\alanh> mkdir jackpot
PS C:\Users\alanh> cd jackpot
Then, start VS Code in that folder by typing the following command:
PS C:\Users\alanh\jackpot> code .
Then, add Hardhat to your projet and start the project:
PS C:\Users\alanh\jackpot> yarn add --dev hardhat
PS C:\Users\alanh\jackpot> yarn add --dev hardhat
Hope this helps. Cheers!
I did unstalled nodejs and delted all unrequired file and folder related to hardhat from C:\Users\user and reinstalled all thing, it worked for me ,

Hexo init fails on Windows

I am trying to initialize a Hexo site on my Windows 10 PC. I have Node 9.9.0 installed as well as git 2.16.2.windows.1.
I have run npm install hexo-cli -g and it installs fine. I then go about the steps listed to create and initialize a site (hexo init test-site) and get the following errors.
I've tried a fresh install of node and git, running npm install a-sync-waterfall -g, and adding a file to node_modules called .a-sync-waterfall.DELETE; but hexo init still fails. Manually running npm install also gives me the same error.
Any ideas? Thanks.
Windows and NPM are not the most stable together. I've found Yarn to much more stable.
Try one of these two things:
Use Yarn
Delete node_modules folder, clear the NPM global cache, attempt a hexo init
del node_modules
npm cache clear --force
hexo init test-site

Running gulp in dreamhost VPS

Since the last dreamhost update which banned VPS users from accessing root/administrator. I am wondering how to do i install GULP in my hosting as it looks like we need admin access to install that and which dreamhost clearly denies it to give access.
So the problem is there are several tasks i need on my server to accomplish via GULP.
Do we have any alternative/Solution to this problem? (except changing the hosting service :))
I'm fed up of not being able to implement my production code in production environment.
thanks
DreamHost VPS are managed servers, they try to strike a balance between giving more computing power and isolation to customers (as opposed to shared hosting) while taking some management burden off of customers hands. This results in a compromise where you don't have to care about the underlying OS and basic services at all, and focus on your applications... but you're limited somewhat to what you can install on the machine.
The good news is that you can manage npm packages as an unprivileged user. While I am not a node expert, I have managed to upgrade npm and install gulp and all dependencies on my local user.
I first created a new dir for my version of npm (mkdir npm) then set the npm prefix to it
$ npm config set prefix ~/npm
Then it's time to install a newer version of npm in that dir:
$ npm install -g npm
After that's done, update the $PATH variable in your .bash_profile and in the current session, too export PATH="$HOME/npm/bin:$PATH" and confirm with a which npm that the npm command bash will execute is /home/$YOUR_VPS_USER/npm/bin/npm.
Now you can install the rest:
npm install -g bower
npm install -g grunt-cli
npm install -g gulp
npm install -g yo
If you need to have more power and accept the responsibilities of running an unmanaged server, DreamHost Cloud is more for you though.