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

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

Related

Unable to run pm2 with npx babel?

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

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\"')

npm script strange behavior using npm terminal

I have this in my package.json
"devDependencies": {
"chromedriver": "^2.37.0",
"geckodriver": "^1.11.0",
"nightwatch": "^0.9.20",
"selenium-server": "^3.11.0"
},
"scripts": {
"e2e": "nightwatch -c tests/nightwatch.conf.js -e chrome"
}
and I'm able to execute npm run e2e
But in my terminal when I do node nightwatch -c tests/nightwatch.conf.js -e chrome I got this error
Error: Cannot find module 'C:\Users\James\Documents\sample_project\nightwatch'
Just curious, what's the problem? I doubt I know how npm and node work now.
When you do 'node nightwatch' it should go into node_modules folder and look for nightwatch, but instead its looking in the root directory.
You could cut the nightwatch folder in node_modules and move it to the same directory as package.json. Give it a try!
Is nightwatch module installed globally? If not, then install this module globally first using npm install nightwatch -g. As you are using this module inside CLI command, hence this must be installed globally in the system.
Sometime there might be issue with npm cache. Try to clean up the npm cache using:
npm cache verify
npm cache clean --force
npm cache clear --force
Then you can run npm install and npm install nightwatch -g again, just to make sure all modules are installed.
Moreover, you can try to use --verbose in your command like:
node nightwatch.js --verbose
And see the output, may be this help to debug the issue.

sh: 1: concurrent: not found while npm start

I am trying to setup angular2 on my local machine, I installed node and npm, after installing npm when I do 'npm start' I am getting below error
root#sameer-Vostro-2520:/home/sameer/angular2/angular-2-beta-boilerplate# npm start
> angular2-boilerplate#1.0.0 start /home/sameer/angular2/angular-2-beta-boilerplate
> concurrent "npm run gulp" "npm run lite"
sh: 1: concurrent: not found
npm ERR! weird error 127
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
npm ERR! not ok code 0
How can we fix this?
installed concurrent but again same error
concurrently#2.2.0 /root/.node/lib/node_modules/concurrently
├── commander#2.6.0
├── bluebird#2.9.6
├── cross-spawn#0.2.9 (lru-cache#2.7.3)
├── moment#2.14.1
├── rx#2.3.24
├── lodash#4.13.1
└── chalk#0.5.1 (supports-color#0.2.0, escape-string-regexp#1.0.5, ansi-styles#1.1.0, strip-ansi#0.3.0, has-ansi#0.1.0)
> angular2-boilerplate#1.0.0 start /home/sameer/angular2/angular-2-beta-boilerplate
> concurrent "npm run gulp" "npm run lite"
/usr/bin/env: node: No such file or directory
npm ERR! weird error 127
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
npm ERR! not ok code 0
This issue got fixed after installing nodejs-legacy
apt-get install nodejs-legacy
First run yarn and then npm start again
Someone who is having difficult running command in your package json file and getting error like
[0] /bin/sh: xxx: command not found
[1] /bin/sh: xxx: command not found
Solution -
Note You are running npm commands via npm so your command should use
npm:
Example -
if you got, following npm command
start, build, lint
You will have concurrently command like this
"scripts: {
"start": "ng start",
"build": "ng build",
"lint": "ng lint"
"run:all": "concurrently npm:start npm:build npm:lint"
}
See in above script usage of npm:

How to use nodemon with .env files?

I am using an .env file to hold environment variables for the server. This works if I run the server with foreman start. But it doesn't work with nodemon.
I would like to use nodemon instead because it restarts automatically when you modify the server. How can I get nodemon to work with .env files?
Install dotenv npm i dotenv
Create .env file and your variables inside
Add the script to execute
"dev": "nodemon -r dotenv/config ./app/index.js " or
"start": "node -r dotenv/config ./app/index.js "
Run the app using npm run dev or npm run start
I have a production Procfile with:
web: node web.js
So I have created a Procfile_dev file with:
web: nodemon web.js
And when I am at development environment I run:
$ foreman start -f Procfile_dev
It works like a charm and doesn't affect production.
You can get nodemon to directly use the .env with the following command
$: env $(cat .env) nodemon app.js
Be aware that you'll have to restart it if you make changes to .env and it won't like it if there are any spaces in your .env file.
With recent versions of Node (since io.js 1.6), you can pass it the -r flag to require a module on start. This lets you directly load .env by using nodemon's --exec:
nodemon --exec 'node -r dotenv/config'
This requires the npm package dotenv to be installed.
Place your local configuration variables in the .env file and run foreman along with nodemon using the following command
$ foreman run nodemon web.js
This works pretty well for me so far,
nodemon -w . -w .env index.js
How it works:
"-w ." tells nodemon to watch the files in the current directory
"-w .env" tells nodemon to watch the .env file
"index.js" is just the file to run when changes occur (could be anything)
"scripts": {
"start": "node -r dotenv/config src/server.js dotenv_config_path=dev.env dotenv_config_debug=true",
"start:dev": "nodemon --exec \"npm start\""
}
In my case the .env file is used for development and not deployment. So I wanted my code to be decoupled from the .env file. Ideally I didn't want to import 'dotenv/config' anywhere in my code. This is my solution:
My nodemon config:
{
"watch": [
"src",
".env"
],
"ext": ".ts",
"exec": "ts-node -r dotenv/config ./src/index.ts"
}
My NPM script:
"start:dev": "nodemon"
In this solution ts-node requires dotenv, which sets up the environment variables before the main app starts. This means that nowhere in my code do I need a import 'dotenv/config'. dotenv can become a dev dependency, and this also prevents dotenv to be loaded at all once the code is deployed.
Thread necromancy!
Use grunt-env to load environmental variables from your heroku config.
In Three steps
Creating the file on root folder > .env
# .env ======
PORT=5000
WHO_AM_I="Who Knows"
Install the dotenv
Run below command
"dev": "nodemon -r dotenv/config src/app.js"
You can access the your defined variables using > process.env.varible_name
If you want to run Typescript in nodemon and require a particular .env file with dotenv then you can do:
In package.json scripts:
"dev": "nodemon -r dotenv/config src/myApp.ts dotenv_config_path=/path/to/your/env/file",
And a line in nodemon.json to tell nodemon to use ts-node when encountering Typescript extensions:
"execMap": {"ts": "node -r ts-node/register"},
This is useful for using a development .env file say .env.development.local for local dev work and leave the main .env file for live production variables.
Use the -w key to specify nodemon what to watch additionally.
"scripts": {
"dev": "env-cmd nodemon -w app -w *.js -w .env server.js"
}
Don't forget rerun npm run dev
Heroku Procfile
Change: web: node app.js to web: nodemon app.js
To load the dotenv package and any declared .env vars into the environment, you can do the following:
nodemon -r dotenv/config myapp.js
I use cross-env for environments.
npm i cross-env
set package.json.
"start": "cross-env NODE_ENV=production node dist/app.js",
"dev": "cross-env NODE_ENV=dev nodemon --exec ts-node src/app.ts",
npm run start OR npm run dev

Categories