PM2 crashes while using ES modules in nodejs - node.js

Ive enabled ES modules in pacakage.json with this key value pair "type": "module" i dont have to use the 'experimental' flag with the latest version of node
when i run pm2 start app.js --watch the app crashes with the error message
0|app | Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader
0|app | at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:781:11)
0|app | at Loader.resolve (internal/modules/esm/loader.js:85:40)
0|app | at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
0|app | at Loader.import (internal/modules/esm/loader.js:164:28)
0|app | at importModuleDynamically (internal/modules/cjs/loader.js:1194:27)
0|app | at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:30:14)
0|app | at Object.<anonymous> (C:\Users\username\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:29:24)
0|app | at Module._compile (internal/modules/cjs/loader.js:1251:30)
0|app | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
0|app | at Module.load (internal/modules/cjs/loader.js:1100:32) {
0|app | code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
0|app | }
the app works perfectly fine without pm2. im guessing pm2 does not support ESM yet!
any workarounds or am i missing some configurations?

Try to pass harmony as node arguments as it will enable es6 features in pm2 process.
pm2 start my_app.js --watch --node-args="--harmony"

If you are on windows 10
pm2 not working with esm on windows 10, there are no workarounds!

Related

create-react-app react-scripts start - ajv-errors: NOT SUPPORTED: option jsonPointers. Deprecated jsPropertySyntax can be used instead

Seeing this error after coming back to a project after a few weeks. Changed/updated nothing, everything was working fine last time I had this laptop open. The following error happens in a container when I'm running docker-compose up.
yarn run v1.22.17
client | $ react-scripts start
client | NOT SUPPORTED: option jsonPointers. Deprecated jsPropertySyntax can be used instead.
client | /app/node_modules/ajv-errors/index.js:4
client | if (!ajv._opts.allErrors) throw new Error('ajv-errors: Ajv option allErrors must be true');
client | ^
client |
client | TypeError: Cannot read properties of undefined (reading 'allErrors')
client | at module.exports (/app/node_modules/ajv-errors/index.js:4:18)
client | at Object.<anonymous> (/app/node_modules/webpack-dev-server/node_modules/schema-utils/src/validateOptions.js:22:1)
client | at Module._compile (node:internal/modules/cjs/loader:1103:14)
client | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
client | at Module.load (node:internal/modules/cjs/loader:981:32)
client | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
client | at Module.require (node:internal/modules/cjs/loader:1005:19)
client | at require (node:internal/modules/cjs/helpers:102:18)
client | at Object.<anonymous> (/app/node_modules/webpack-dev-server/node_modules/schema-utils/src/index.js:7:25)
client | at Module._compile (node:internal/modules/cjs/loader:1103:14)
client | error Command failed with exit code 1.
client | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
client exited with code 1
So posting my own question I changed how I was looking at the problem.
I came across this question/answer AJV and ajv-formats latest must be broken in React
I found the answer from #Tsar Bomba about installing
npm install ajv#7.2.3 ajv-errors#2.0.1 ajv-formats#2.1.1 --save
Worked for me and I was able to serve my project again
The following also worked for me.
npm install ajv#7.2.3 ajv-errors#2.0.1 ajv-formats#2.1.1 --save

Download or set Heroku app config variables while running the app locally

I have an app which I have downloaded from GitHub and after pushing it to Heroku it works perfectly fine. however when I use the heroku local command to run it locally it throws an error regarding configuration variables missing.
How can I download these config files to my local folder (do I even need to)? Do I need to create an .env file manually in order to make it work?
Here is the app:
const assert = require('assert');
const Provider = require('oidc-provider');
assert(process.env.HEROKU_APP_NAME, 'process.env.HEROKU_APP_NAME missing');
assert(process.env.PORT, 'process.env.PORT missing');
assert(process.env.SECURE_KEY, 'process.env.SECURE_KEY missing, run `heroku addons:create securekey`');
assert.equal(process.env.SECURE_KEY.split(',').length, 2, 'process.env.SECURE_KEY format invalid');
// new Provider instance with no extra configuration, will run in default, just needs the issuer
// identifier, uses data from runtime-dyno-metadata heroku here
const oidc = new Provider(`https://${process.env.HEROKU_APP_NAME}.herokuapp.com`, {
clients: [
{
client_id: 'foo',
redirect_uris: ['https://jwt.io'], // using jwt.io as redirect_uri to show the ID Token contents
response_types: ['id_token'],
grant_types: ['implicit'],
token_endpoint_auth_method: 'none',
},
],
cookies: {
keys: process.env.SECURE_KEY.split(','),
},
});
// Heroku has a proxy in front that terminates ssl, you should trust the proxy.
oidc.proxy = true;
// listen on the heroku generated port
oidc.listen(process.env.PORT);
and here is the error I get when I run "heroku local":
3:44:57 AM web.1 | assert.js:383
3:44:57 AM web.1 | throw err;
3:44:57 AM web.1 | ^
3:44:57 AM web.1 | AssertionError [ERR_ASSERTION]: process.env.HEROKU_APP_NAME missing
3:44:57 AM web.1 | at Object.<anonymous> (C:\Users\hmffa\Desktop\my-provider\src\index.js:4:1)
3:44:57 AM web.1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
3:44:57 AM web.1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
3:44:57 AM web.1 | at Module.load (internal/modules/cjs/loader.js:928:32)
3:44:57 AM web.1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
3:44:57 AM web.1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
3:44:57 AM web.1 | at internal/main/run_main_module.js:17:47 {
3:44:57 AM web.1 | generatedMessage: false,
3:44:57 AM web.1 | code: 'ERR_ASSERTION',
3:44:57 AM web.1 | actual: undefined,
3:44:57 AM web.1 | expected: true,
3:44:57 AM web.1 | operator: '=='
3:44:57 AM web.1 | }
[DONE] Killing all processes with signal SIGINT
3:44:57 AM web.1 Exited with exit code null
These variables often vary between environments. Some things, like your database connection string and security keys, should be different locally and in production.
However, Heroku documents an easy way to copy a config var from Heroku to a local .env file:
Sometimes you may want to use the same config var in both local and Heroku environments. For each config var that you want to add to your .env file, use the following command:
heroku config:get CONFIG-VAR-NAME -s >> .env
Do not commit the .env file to source control. It should only be used for local configuration. Update your .gitignore file to exclude the .env file.
Note that .env files aren't magic. Node.js won't use the values in there out of the box.
If you use heroku local to run your app locally (as you appear to be) you should find that your .env file gets used. If you run it another way, you may need to do additional work, e.g. perhaps by adding dotenv.

Strapi develop script not working with PM2

I am trying to run Strapi in development mode with pm2 by using this command:
pm2 start npm --name myprojectname -- run develop
Unfortunately, I get the following error
0|edtech-a | SyntaxError: Unexpected token ':'
0|edtech-a | at wrapSafe (internal/modules/cjs/loader.js:979:16)
0|edtech-a | at Module._compile (internal/modules/cjs/loader.js:1027:27)
0|edtech-a | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
0|edtech-a | at Module.load (internal/modules/cjs/loader.js:928:32)
0|edtech-a | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
0|edtech-a | at Object.<anonymous> (C:\Users\dimit\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:33:23)
0|edtech-a | at Module._compile (internal/modules/cjs/loader.js:1063:30)
0|edtech-a | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
0|edtech-a | at Module.load (internal/modules/cjs/loader.js:928:32)
0|edtech-a | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
PM2 version is 4.5.1
NodeJS version is 14.15.4
NPM version is 6.13.2
Strapi version is 3.1.0-alpha.5
I get the same error when I try to run Strapi in development mode through an ecosystem.config.json file as well.
Has somebody faced this issue, if yes, how can I solve it?
Yeah, I encountered some problems myself and decided to go for a workaround:
Create a script with the commands you would type normally:
touch strapi.sh
Type in your desired commands:
nano strapi.sh
cd my-strapi-project
npm run develop
Close and save (Crtl+x then type "y" and press enter).
Now just start that file with pm2
pm2 start strapi.sh
Check if everything works fine
pm2 logs
Thats it.
Can you try with this:
pm2 start npm --name myprojectname --interpreter bash -- run develop
UPDATED
for yarn
pm2 start yarn --name myprojectname --interpreter bash -- run develop

"SyntaxError: Invalid or unexpected token" when running node-red using PM2 on Win ES 7

Node-red version: 0.19.5
PM2 version: 3.2.3
OS: Windows Embedded Standard 7 (Running on ICO300, with 8gb of RAM)
Steps to reproduce
Install node-red and pm2 on Windows ES 7
Run node red through pm2: pm2 start node-red
Check node-red logs: pm2 logs node-red
Result
I got the following error:
0|node-red | SyntaxError: Invalid or unexpected token
0|node-red | at new Script (vm.js:79:7)
0|node-red | at createScript (vm.js:251:10)
0|node-red | at Object.runInThisContext (vm.js:303:10)
0|node-red | at Module._compile (internal/modules/cjs/loader.js:656:28)
0|node-red | at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
0|node-red | at Module.load (internal/modules/cjs/loader.js:598:32)
0|node-red | at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
0|node-red | at Function.Module._load (internal/modules/cjs/loader.js:529:3)
0|node-red | at Object.<anonymous> (c:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:27:21)
0|node-red | at Module._compile (internal/modules/cjs/loader.js:688:30)
0|node-red | C:\USERS\ADMINISTRATOR\APPDATA\ROAMING\NPM\NODE-RED.CMD:1
0|node-red | (function (exports, require, module, __filename, __dirname) { #IFEXIST "%~dp0\node.exe" (
and node-red does not run.
Running node-red directly from CMD can be done normally (except that after 1 hour it usually crashes (cause discussed here, still unsolved), that's why as a workaround I want to use PM2 to auto-restart it.)
On Windows, the node-red.cmd is not a valid, you have to run directly the Node.js command instead:
pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js

node Issue in Creating demo project using react_on_rails gem

I am trying to follow this tutorial to create a dummy project using gem react_on_rails
I guess I have followed every step of the tutorial but on the last step where we have to run the project a node js related error is popping up.
I even updated my node version on the system, still facing the same issue
Error
foreman start -f Procfile.dev
22:30:58 web.1 | started with pid 5370
22:30:58 client.1 | started with pid 5371
22:30:59 client.1 | (in /home/projects/test-react-on-rails)
22:31:00 web.1 | => Booting Puma
22:31:00 web.1 | => Rails 5.1.2 application starting in development on http://localhost:3000
22:31:00 web.1 | => Run `rails server -h` for more startup options
22:31:00 web.1 | Puma starting in single mode...
22:31:00 web.1 | * Version 3.9.1 (ruby 2.4.0-p0), codename: Private Caller
22:31:00 web.1 | * Min threads: 5, max threads: 5
22:31:00 web.1 | * Environment: development
22:31:00 web.1 | * Listening on tcp://localhost:3000
22:31:00 web.1 | Use Ctrl-C to stop
22:31:01 client.1 | yarn run v0.27.5
22:31:01 client.1 | $ NODE_ENV=development webpack -w --config webpack.config.js
22:31:01 client.1 | /home/projects/test-react-on-rails/client/webpack.config.js:6
22:31:01 client.1 | const {resolve } = require('path');
22:31:01 client.1 | ^
22:31:01 client.1 |
22:31:01 client.1 | SyntaxError: Unexpected token {
22:31:01 client.1 | at exports.runInThisContext (vm.js:53:16)
22:31:01 client.1 | at Module._compile (module.js:373:25)
22:31:01 client.1 | at Object.Module._extensions..js (module.js:416:10)
22:31:01 client.1 | at Module.load (module.js:343:32)
22:31:01 client.1 | at Function.Module._load (module.js:300:12)
22:31:01 client.1 | at Module.require (module.js:353:17)
22:31:01 client.1 | at require (internal/module.js:12:17)
22:31:01 client.1 | at requireConfig (/home/shubhangi/projects/test-react-on-rails/client/node_modules/webpack/bin/convert-argv.js:96:18)
22:31:01 client.1 | at /home/projects/test-react-on-rails/client/node_modules/webpack/bin/convert-argv.js:109:17
22:31:01 client.1 | at Array.forEach (native)
22:31:01 client.1 | error Command failed with exit code 1.
22:31:01 client.1 | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
22:31:02 client.1 | exited with code 1
22:31:02 system | sending SIGTERM to all processes
22:31:02 web.1 | - Gracefully stopping, waiting for requests to finish
22:31:02 web.1 | === puma shutdown: 2017-07-27 22:31:02 +0530 ===
22:31:02 web.1 | - Goodbye!
22:31:02 web.1 | Exiting
22:31:02 web.1 | exited with code 0
You're trying to use object destructuring, which was first supported in Node version 6, and is not available in Node 4.
You should upgrade your Node version, especially since version 4 is no longer actively maintained, which means that only critical fixes are applied, until it reaches the end of life (for details see LTS schedule).
If you still have to use Node 4, you cannot use object destructuring and have to change the line to:
const resolve = require('path').resolve;
Another possibility would be to transpile the unsupported features with Babel, but that seems a little heavy handed for a config.

Resources