Node.js application not executing in production env - node.js

I am new in node.js so unable to understand error properly. As per the sails document https://sailsjs.com/documentation/concepts/deployment
I am trying to execute a sails.js application with NODE_ENV=production node app.js but it is throwing some errors. While it is working fine with for development environment with npm run dev
For now disabled session in my project. Now other warnings has been removed.
$ NODE_ENV=production node app
error: ** Grunt :: An error occurred. **
error:
------------------------------------------------------------------------
Aborted due to warnings.
Running "babel:dist" (babel) task
Warning: .tmp/public/js/sdk.js: 'with' in strict mode (22:780)
------------------------------------------------------------------------
error: Looks like a Grunt error occurred--
error: Please fix it, then **restart Sails** to resume watching for changes to assets:
error: CTRL+C, then `sails lift`
error: Or if you're stuck, check out the troubleshooting tips below.
error: Troubleshooting tips:
error:
error: *-> Are "grunt" and any custom Grunt plugins you're using installed in this project?
error: Run `npm install` if you're not sure.
error:
error: *-> You might have a typo in one of your LESS files, etc.
error:
error: *-> Or maybe you don't have permissions to access the `.tmp` directory?
error: e.g., `C:\xampp\htdocs\soochna_sails\.tmp` ?
error:
error: If you think this might be the case, try running:
error: sudo chown -R YOUR_COMPUTER_USER_NAME C:\xampp\htdocs\soochna_sails\.tmp
error:
error: *-> If you're still unsure, drop by https://sailsjs.com/support.
my package.json file is
"description": "a Sails application",
"keywords": [],
"dependencies": {
"#sailshq/connect-redis": "^3.2.1",
"#sailshq/lodash": "^3.10.3",
"#sailshq/socket.io-redis": "^5.2.0",
"async": "2.0.1",
"grunt": "1.0.1",
"nodemon": "^1.17.5",
"sails": "^1.0.2",
"sails-hook-autoreload-extend": "^1.0.4",
"sails-hook-grunt": "^3.0.2",
"sails-hook-orm": "^2.0.0-16",
"sails-hook-sockets": "^1.4.0",
"sails-mysql": "1.0.0",
"dropzone": "5.5.1"
},
"devDependencies": {
"#sailshq/eslint": "^4.19.3"
},
"scripts": {
"start": "NODE_ENV=production --ignore 'tmp/*' node app.js",
"dev": "set NODE_ENV=development && nodemon --ignore 'tmp/*' app.js && exit 0",
"test": "npm run lint && npm run custom-tests && echo 'Done.'",
"lint": "eslint . --max-warnings=0 --report-unused-disable-directives && echo '✔ Your .js files look good.'",
"custom-tests": "echo \"(No other custom tests yet.)\" && echo"
},
"main": "app.js",
"repository": {
"type": "git",
"url": "git://github.com/xxxxxxx/test-project.git"
},
"license": "",
"engines": {
"node": ">=9.11"
}
Any help will be appreciated.

I ran into this issue aswell, quite a lot. This is the interesting part:
error: *-> Or maybe you don't have permissions to access the .tmp directory?
error: e.g., C:\xampp\htdocs\soochna_sails\.tmp ?
error:
error: If you think this might be the case, try running:
error: sudo chown -R YOUR_COMPUTER_USER_NAME C:\xampp\htdocs\soochna_sails.tmp
You are obviously hosting on windows, so try making the .tmp folder write accessible or delete it manually prior to launching your application in production mode.

Related

Build fail in NextJS monorepo — HookWebpackError: processor is not a function

When I try to build one of the NextJS apps in my monorepo, I get the error:
$ rm -r packages/library/node_modules/; rm -r packages/daily/node_modules/; rm -r node_modules/; rm -r packages/shared/node_modules; rm package-lock.json; sudo rm -r packages/daily/.next/; npm i; npm --prefix packages/daily/ run build
> next build
info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
info - Checking validity of types
info - Creating an optimized production build
Failed to compile.
HookWebpackError: processor is not a function
> Build error occurred
Error: > Build failed because of webpack errors
at /home/ubuntu/cal-frontend/packages/daily/node_modules/next/dist/build/index.js:397:19
at async Span.traceAsyncFn (/home/ubuntu/cal-frontend/packages/daily/node_modules/next/dist/telemetry/trace/trace.js:60:20)
at async Object.build [as default] (/home/ubuntu/cal-frontend/packages/daily/node_modules/next/dist/build/index.js:77:25)
The strange thing is that the node/npm version and code are exactly the same as a few days ago when I last ran this command successfully. I have never had this error before. I even tried cloning to a brand new Linux EC2 instance to see if there was some cached file screwing it up but I got exact same error. I am still able to re-deploy the latest commit on Heroku from a few days ago.
The next dev command works fine still.
The issue started happening when I tried to create a new website under packages/ . But the error is persisting even after I reverted to a commit before I made that code.
The monorepo contains two websites that share a package of shared code inside the repo called shared. Here is the webpack next.config.js file for the package/daily app. Again, no code has changed since the error started.
const withFonts = require('next-fonts');
const withTM = require('next-transpile-modules')(['#my-repo-name/shared']);
require('dotenv').config({ path: '../../.env' });
module.exports = withTM(withFonts({
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
webpack(config, options) {
return config;
},
}));
node version: v16.10.0
npm version: 7.24.0
(I also tried building using 16.14 and 8.3, and had the same error)
Also the root level package.json (with personal details redacted):
{
"name": "my-repo-name",
"version": "1.0.0",
"workspaces": {
"packages": [
"packages/*"
]
},
"keywords": [],
"author": "redacted-for-stack-overflow#stackoverflow.org",
"license": "UNLICENSED",
"description": "REDACTED FOR STACK OVERFLOW",
"engines": {
"node": "16.10.0"
},
"cacheDirectories": [
"node_modules",
"packages/library/.next/cache"
],
"scripts": {
"lint": "eslint . --ext js,jsx",
"build": "npm run build --prefix packages/$APP_NAME"
},
"dependencies": {},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.32.0",
"eslint-config-next": "^11.1.2",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0"
}
}
There was an incident yesterday which was causing the issue at Vercel. It should be fixed now.
Adding resolutions keys in package.json solved this for me. Hope this should be a temporary issue with vercel.
"resolutions": {
"cssnano-preset-simple": "3.0.0"
}

how to use forever with npm

i am trying to run this command in my debian 10 vps forever start npm start but i got this error in the terminal,
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: npm
(node:23858) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23858) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
error: Cannot start forever
error: script /root/node_projects/tiktok/backend/npm does not exist.
Here is my package.json file
{
"name": "auth-graphql",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"engines": {
"node": "10.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node -r esm ./src/index.js",
"start": "nodemon -r esm ./src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
...
"esm": "^3.2.0",
...
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"nodemon": "^1.18.4"
}
}
i tried
forever start esm ./src/index.js
forever start -c "npm start" ./
forever start -c "npm start" /path/dir/
forever --sourceDir /path/dir/ -c "npm start" /
and i also got almost the same error, Please I need help, How can i solve this problem
try this command:
forever start -c "npm start" ./
running your application's specific directory:
forever start -c "npm start" /path/dir/
or use this command:
forever --sourceDir /path/dir/ -c "npm start" /
I've had similar problems and I think it may be some sort of incompatibility with NPM/Node versions (see discussion here.
I switched to https://github.com/Unitech/pm2 instead and things started working immediately.
I don't know the solution but. The problem is related to a circular dependency. Node version 14+ is no longer supports circular dependency. Example
In fileA
var fileB = require(fileB);
In fileB
var fileA = require(fileA);

NextJS cannot find a valid build in the '.next' directory

I looked at the following question before asking this one but I believe mine is different because I am not using Docker: Nextjs fails to find valid build in the '.next' directory in production node_env
I also tried this approach of removing the '.next' folder but still get the same issue.
After fixing a host of other issues, I am down to one I cannot seem to resolve. When I try to deploy to Heroku I keep getting the following error:
node server.js
Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.
Here is my package.json file:
{
"name": "StarterApp",
"version": "1.0.0",
"engines": {
"node": "10.4.1"
},
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"dev": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "4.16.3",
"fs-extra": "^5.0.0",
"ganache-cli": "^6.1.3",
"mocha": "^5.2.0",
"next": "^4.2.3",
"next-routes": "^1.4.2",
"node-gyp": "^3.7.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"rebuild": "^0.1.2",
"semantic-ui-css": "^2.3.2",
"semantic-ui-react": "^0.79.1",
"sha3": "^1.2.2",
"solc": "^0.4.24",
"truffle-hdwallet-provider": "0.0.3",
"web3": "^1.0.0-beta.34"
}
}
Server.js file:
const { createServer } = require('http');
const next = require('next');
const app = next({
dev: process.env.NODE_ENV !== 'production'
});
const routes = require('./routes');
const handler = routes.getRequestHandler(app);
app.prepare().then(() => {
createServer(handler).listen(5000, (err) => {
if (err) throw err;
console.log('Ready on localhost:5000');
});
});
The app deploys without issue locally but I get this error when deploying to Heroku. What am I doing wrong?
npm run build
then
npm run start
solved my problem.
First
npm run-script build
Then
npm run start
Just see the error carefully:
Error: Could not find a production build in the 'E:\Developer's Area\weatherteller\.next' directory. Try building your app with 'next build' before s at Server.readBuildId (E:\Developer's Area\weatherteller\node_modules\next\dist\next-server\server\next-server.js:146:355)
at new Server (E:\Developer's Area\weatherteller\node_modules\next\dist\next-server\server\next-server.js:3:120)
at createServer (E:\Developer's Area\weatherteller\node_modules\next\dist\server\next.js:2:638)
at start (E:\Developer's Area\weatherteller\node_modules\next\dist\server\lib\start-server.js:1:323)
at nextStart (E:\Developer's Area\weatherteller\node_modules\next\dist\cli\next-start.js:19:125)
at E:\Developer's Area\weatherteller\node_modules\next\dist\bin\next:27:115
while running
npm start
It's not able to locate the production build which is required to launch the next app. While creating next app using
npm install next react react-dom --save
.next folder was not created so you need to create the .next folder first using
npm build
which will consist of all your production build files.
After npm build the folder will be created and you can run your app using
npm start
Also, make sure these scripts are in your next app
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
Hope this resolves your error 😀😀
I had the same issue once.
First, remove package-lock.json or yarn.lock files and node_modules folder and make sure you install all the packages again with the yarn command.
I had to run yarn build first and then the yarn start command was working fine afterward.
Also if the currently used port is taken by another app you will have issues running your dev environment. You can easily fix that by going into the package.json file and modifying the "dev" script like this: "dev": PORT=7080 next dev".
Also possibly you are mixing Next.js with React because in Next.js you should use yarn dev to start your project for development instead of yarn start.
I hope this was helpful to you.
NextJS building may be (depending on your project size), be extremely large, something that can cost you money during deploys.
You can apply the following to your package.json
{
"script": {
"build": "next build",
"heroku-postbuild": "npm run build",
"start": "next start"
}
}
I was getting this error when trying to start a production server from the build directory after setting.
distDir: 'build',
actual error
Error: Could not find a production build in the '/home/username/awesome-app/build/build' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
and my start command
$ next start build
so I moved the next build directory to another directory named build and it works, and if you are using environment variable put that file inside the build directory.

How to install man on Heroku

My app needs the package: man, but by default it's not installed on the heroku/nodejs buildpack.
So according to the documentation, heroku/heroku-buildpack-apt is the tool for the job when your app needs additional apt dependencies.
I assigned the new buildpack and added a Aptfile to the root of the project with one line:
man
Here is my full package.json
{
"name": "unix-translator",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"test": "mocha --exit"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"node-dev": "^3.1.3",
"serve-favicon": "~2.4.5",
"dateTools": "file:lib/unix-command"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^4.1.0"
}
}
Here's my Procfile:
web: node ./bin/www
This gets the dependency successfully installed because I see it when I run which man now. But it doesn't work.
I get this error when I try to use the man program:
~ $ man cat
man: error while loading shared libraries: libmandb-2.7.5.so: cannot open shared object file: No such file or directory
I did found this blog post and this other blog post which both suggest the problem related to permissions and the location of files... I SSHed into my dyno and ran: /sbin/ldconfig -v and it eventually threw this error:
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Read-only file system
^ The command needs to be run with sudo and that's not available inside a dyno. :-(
So I'm stuck again.
Not 100% sure, but this might be worth a shot:
Replace the "prestart" in your package.json with heroku-prebuild (or heroku-postbuild), as follows:
"scripts": {
"start": "node ./bin/www",
"test": "mocha --exit",
"heroku-prebuild": "apt-get update && apt-get install man"
},
You had this in "prestart", which means it gets executed when you run npm start.
However, from your question, it looks like you are accessing a one-off Heroku dyno (e.g. with "heroku run bash"), and then trying to run "man cat" therein. So when you do that, you are not running "npm start" at all on your dyno.
By putting the "apt-get" in one of the Heroku specific build steps, it executes when your slug gets built, and hence whatever you install should be available on any dyno in your app (including one-off dynos).

Heroku - error running browserify on Node deployment

I'm trying to deploy a Node app to Heroku, but I'm having an issue successfully running browserify when the app is deployed.
When I'm running locally, I browserify my script with npm run bundle like so (from package.json):
"bundle": "./node_modules/browserify/bin/cmd.js build/main.js -o public/scripts/bundle.js
which browserifies the script in build/main.js and puts it into public/scripts/bundle.js.
For deploying to Heroku, I added
"postinstall": "npm run bundle"
However, when I deploy, I get the following error:
Error: ENOENT: no such file or directory, open 'public/scripts/bundle.js.tmp-browserify-59309133185877094263'
Well, that's correct, that file shouldn't exist... yet. When I run npm run bundle locally, I do see that file briefly pop into existence, but then it is quickly removed and I'm left with a nice updated bundle.js.
I read through Heroku's docs on this, but I'm miffed... can anyone clarify how to get through this?
For reference, here are the relevant parts of my package.json:
"scripts": {
"bundle": "./node_modules/browserify/bin/cmd.js build/main.js -o public/scripts/bundle.js",
"postinstall": "npm run bundle"
},
"dependencies": {
"body-parser": "^1.17.1",
"browserify": "^14.1.0",
"ejs": "^2.5.6",
"express": "^4.15.2",
"jquery": "^3.2.1",
"path": "^0.12.7",
"superagent": "^3.5.2"
},
"devDependencies": {},
"engines": {
"node": "6.8.1",
"npm": "4.0.5"
}
Solved! I had bundle.js included in my global gitinore configuration. Just had to take that out, good to go!

Resources