nodemon not loading environment variables - javascript

The following is in my gulpfile.js:
const nodemon = require('nodemon')
const server = nodemon({
script: './src/index.js',
ext: 'hbs json js css',
require: 'dotenv/config',
dotenv_config_path: './config/.env',
stdout: false // without this line the stdout event won't fire
})
I am looking to duplicate the exact same behavior as the dev script in the package.json file:
"scripts": {
"test": "jest --watch",
"dev": "nodemon -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css",
"debug": "nodemon --inspect -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css"
},
For one reason or another it seems that gulp is not registering the environment variables defined in the .env file. Although the line below works just fine when ran with npm run dev:
nodemon -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css

You may need to add export to your environment vars and load them before running nodemon:
vars.env
export ENV_FIRST="J"
export ENV_LAST="Doe"
package.json
"scripts": {
"dev": "source vars.env; nodemon ..."
...
}

Related

PKG Failed to make Bytecode

I am getting this warning when I try and run .. pkg index.js -t macOS
node v17.3.1
pkg#5.5.2
Warning Failed to make bytecode node17-arm64 for file /snapshot/______/index.js
was hoping anyone could help,
I have also tried to use -b and got
Error: ENOENT: no such file or directory, open '/var/folders/fy/c5tgsjcj63q73kfvg_dd53fh0000gn/T/pkg.d5ef9dd92b18360a4ff95824/node/out/Release/node
thank you
My Script was written in ES6, I ran it threw Babel and then tried again and it worked perfectly!!
I used #vercel/ncc to generate one index.js file and then use pkg on the generated index.js file.
My package.json scripts is like this:
"scripts": {
"test": "jest",
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"build": "ncc build -o build/lib ./src/index.js && pkg --target node16-win-x64 -o build/MY-API.exe ./build/lib/index.js"
},

How to configure mocha to support ES6 with node v13 and bable v6?

How to configure test setup with mocha to support es6 with babel 6?
I tried to configure with --require option with mocha as as mentioned here but it does not resolve my problem
I have also tried with --compiler option but it does not support with mocha v7
Node v13.13.0
Babel v6.26
mocha v7.1.1
> mocha './test/**/*.js' --require babel-core/register
import module from '../models/module';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Module is a .js files
babelConfig.babelrc file contains
{
"presets": ["env"]
}
pakcage.json configrations of scripts section:
"scripts": {
"start": "nodemon server.js --exec babel-node --presets env",
"release": "npm run clean && npm run build && npm run serve",
"clean": "rm -rf dist && mkdir dist",
"build": "babel . -s -D -d dist --presets env --ignore node_modules",
"serve": "node dist/server.js",
"test": "mocha './test/**/*.js' --require babel-core/register"
}

Using nodemon and --inspect with serverless

I am writing a lambda service and hate having to stop and start the server after every change.
I do have this running together with chrome --inspect working correctly:
"start": "node --inspect-brk $(which serverless) invoke local -f getGoldenDeomondotcom"
I then used:
"start": "nodemon --exec \"node --inspect-brk $(which serverless) invoke local -f getGoldenDeomondotcom\""
to allow me to use nodemon with --inspect, however the messages don't pipe through to chrome.
Any ideas how I could use them together?
Make sure you use js or replace js in the code below with your language ext e.g py,ts
in your package.json
"scripts": {
"start:dev": "nodemon -e js --exec \"sls offline\""
},
From github ... (https://github.com/akupila)
nodemon --exec "serverless serve start"
I'm using this in package.json during dev:
"scripts": {
"start": "nodemon --exec \"serverless serve start\""
}
This allows me to just run npm start. Whenever any file in the project changes nodemon will restart serve.
I'm using serverless-offline and serverless-dynamodb-local, I just added
"start": "nodemon --exec \"sls offline start\"" to scripts

How can I run multiple NPM commands with a single NPM Command on Windows

I am setting a package.json file that will start Nodemon, run my watch css command and run browser sync all with the "npm start" command.
This works on my Mac computer at work but does not work on my Windows computer at home.
On my Mac, it will listen for any changes to server and SCSS files and also run browser sync.
On Windows it only runs Nodemon and just waits for any server changes. It looks like it ignores my other two commands.
Do I need to do something different for Windows? Ideally I would like the code to work universally on both platforms.
Nodemon seems to be the problem here because watch-css css and browsersync work but anything after nodemon does not work.
"scripts": {
"build-css": "node-sass --output-style compressed --source-map true -o public/css scss",
"watch-css": "nodemon -e scss -x \"npm run build-css\"",
"build-js": "browserify js/app.js -o public/js/app.js",
"browser-sync": "browser-sync start --port 4000 --proxy 'localhost:3000' --files 'views/*' 'public/**/*' --no-notify",
"start": "nodemon ./bin/www & npm run watch-css & npm run browser-sync"
},
Here's what I use: npm-run-all(this is cross-platform). They allow you to run your processes/commands parallelly and sequentially (-p or -s).
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/ --importer=node_modules/node-sass-tilde-importer",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
// ... and much more
}
This is working fine for me in both windows and mac. Try it, hope this is helpful.

mocha Error: No test files found: "test/" npm ERR! Test failed

I am trying to run mocha tests on linux >
# test C:\Users\wd.ssh\tel\qa_test\mocha-api-tests
Error: No test files found: "test/"
npm ERR! Test failed. See above for more details.
here is my package.json
{
"scripts": {
"test": "mocha --reporter mocha-junit-reporter --timeout 60000 --exit",
},
"dependencies": {
"cassandra-driver": "3.5.0",
"chai": "4.2.0",
"chai-http": "4.2.0",
"express": "4.16.4",
"mocha-junit-reporter": "1.18.0",
"request-promise": "4.2.2"
}
}
commands used:
npm install --global mocha
npm i
and to run the tests I'm using npm test
Project structure:
seeing your project structure, seems that you have one test.js so mocha should target this file.
"scripts": {
"test": "mocha test.js --reporter mocha-junit-reporter --timeout 60000 --exit",
},
If you want to add more test files, it is better to put them inside test directory e.g. /test and must change the target to test files inside the directory.
"scripts": {
"test": "mocha 'test/**/*.js' --recursive --reporter mocha-junit-reporter --timeout 60000 --exit",
},
Hope it helps
you need to provide the path to the test folder. try this:
"scripts": {
"test": "mocha qa_test/**/*.js --reporter mocha-junit-reporter --timeout 60000 --exit",
},
npm test
If there is no test file directly in the test folder, you will get that error.
Solution is add "--recursive" option to your test in the package.json.
"scripts": {
"test": "mocha --recursive --reporter mocha-junit-reporter --timeout 60000 --exit",
},
this will tell mocha that "look recursively for the test files"
Mocha automatically searches for folder called test and should be in root folder as package.json
I fixed the same problem ...
"test": "mocha -r ts-node/register 'tests/**/*.ts'"
One reason this happened to me with saving the test file without extension. Yep, I did it once and Mocha couldn't detect the file and run the tests. Make sure it has at least .js as extension. Notice that some IDEs may already show the file as JS (as it detects the file content), but you still need to have the extension of the file as JS.
Also had this problem and this worked for me:
./node_modules/mocha/bin/mocha -r esm -r ts-node/register "src/**/*Test.ts"

Categories