Unable to run pm2 with npx babel? - javascript

I have a Node.js app that perfectly works with the following command:
npx babel-node dist/index.js
However, I'm not able to run this with pm2. When I do the following:
pm2 start --interpreter npx babel-node dist/index.js
Obviously, this does not work. And when I try:
pm2 start --interpreter babel-node dist/index.js
This does not work either, as my global babel is version 6.26.3 and my project babel is 7.0.0-0.
Is there a way to make pm2 to play nice with npx or maybe there is a way to upgrade the system wide babel to 7.0?

To solve this you need to create a build and a start script in your package.json:
"scripts": {
"build": "npx babel src -d dist",
"start": "npm run build && node dist/index.js"
},
And then run the pm2 using the start option:
pm2 start npm --name "app name" -- start

Related

How to run npm script (package.json) written in an external js file?

I know it has been covered in different questions but mine is a bit different:
Sorry in advance if it sounds really noob.
this is the script in package.json:
"start": "nodemon ./index.js --exec \"node -r babel-register\"",
I replaced that with:
"start": "node scripts/start.js",
and in start.js, I do:
const { execSync } = require('child_process')
execSync('nodemon ../index.js --exec \"node -r babel-register\"')
which throws an error:
/bin/sh: nodemon: command not found
Am I right with "execSync"?
I tried import nodemon in the file but it is obviously not helping.
What you're doing should work if nodemon is installed globally, i.e. with:
npm install -g nodemon
But if it's installed as a project dependency, i.e. with:
npm install --save-dev nodemon
Then you'll need to run it from the directory containing all the locally installed binaries: node_modules/.bin/
So something like this should work:
execSync('./node_modules/.bin/nodemon ../index.js --exec \"node -r babel-register\"')

Nodejs: How to prepare code for production environment

I'm a beginner developing with Nodejs and React.
Right now, I've got a first version of my web application which works correctly in development environment, but I'm trying to build a version for production environment but I've got this error
ReferenceError: document is not defined
The scripts of my package.json are:
"scripts": {
"dev-webpack": "webpack-dev-server --hot --mode development",
"clean": "rm -rf ./dist",
"dev": "npm run build-dev && cross-env NODE_ENV=development nodemon --exec babel-node src/server/server.js --ignore ./src/client",
"build-dev": "npm run clean && npm run compile-dev",
"compile-dev": "NODE_ENV=development webpack -d --config ./webpack.config.babel.js --progress",
"compile": "NODE_ENV=production webpack -p --config ./webpack.config.babel.js --progress",
"build": "npm run clean && npm run compile",
"start": "npm run build && node ./dist/assets/js/bundle.js"
},
And I try to create the version for production environment with the command npm run start
I have been looking for information about the problem and it seems it's due because I have no Browserify my web application. But, I don't know how to do this correctly nor the steps to follow to do it correctly.
I am seeking a list of the steps required to build a correct version for production environment.
Edit I:
These are the static files generated with "build" script:
The React application is designed to be run in a browser.
When you run dev-webpack you are running an HTTP server and pointing a browser at it.
When you run build you are creating a static JavaScript file. You need to deploy it to a web server (along with the associated HTML document) and then point a browser at the HTML document.
You are currently trying to execute bundle.js with Node and not a browser.
You need to serve your index.html file. You can use serve to host the HTML file.

Modifying `babel-register server.js` after updating `babel-register` to `#babel/register`

I am updating the babel packages used in a boilerplate, from babel-core, babel-register, etc., to #babel/core, #babel/register, etc.
Problem: In the npm script dev, it was previously using babel-register to run server.js.
"dev": "concurrently -k 'node -r babel-register server.js' 'npm start'",
After updating the babel-register npm package to #babel/register, the above script throws the error
Error: Cannot find module 'babel-register'
What is the correct command to update babel-register server.js?
It's the same as the other places. You'd want
node -r #babel/register server.js

How to use npm script directly?

A script in my package.json
"start": "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node ./react-imvc/bin/www-babel-register",
I need to start --inspect, for some reason, I can not modify the package.json, such as
"start": "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node --inspect ./react-imvc/bin/www-babel-register"
I want to use script directly, like this :
npm run "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node --inspect ./react-imvc/bin/www-babel-register"
using npm run script, but it failed.
How can I use npm script like above?
assume that you want to run script with arg --inspect without changing the package.json:
npm run start -- --inspect
see:
$ npm run --help
npm run-script <command> [-- <args>...]
alias: run
So you want to execute the script you've got in your package.json which should look like this (after you've added the --inspect flag):
"scripts": {
"start": "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node --inspect ./react-imvc/bin/www-babel-register"
}
This can be done by calling npm run start. Adding more scripts under another key lets you call them by using `npm run '.

Nodejs - npm start & npm build both do not work?

this is a snippet from my package.json
"scripts": {
"start": "mkdir BigDirectory",
"build": "mkdir BigDirectory"
},
I've noticed that npm build simply goes to the next new line in terminal (without creating a directory)
While npm start works and creates the "BigDirectory"
Why is npm build non-responsive? Am i perhaps allowed only one single script in my package.json?

Categories