I have a config file that I specify my database path in that:
{
"runningPort": 3001,
"databasePath":"database.json",
}
The problem is that I use Nodemon and I want to ignore my database file when using it.
my script in package.json is Like this:
{
...,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --ignore database.json start.js"
},
...
}
what I want is to read database.json from config.json file so that just by changing databasePath in config.json file I can change the targeted database.
Related
I am trying to create a Dapp using truffle but when I reach the stage of using the command npm run dev it gives me an error saying MISSING SCRIP. my code is as follows:
{
"name": "eth-vreg",
"version": "1.0.0",
"description": "Blockchain vreg Powered By Ethereum",
"main": "truffle-config.js",
"directories": {
"test": "test"
},
"scripts": {
"dev": "lite-server",
"test": "echo \"Error: no test specified\" && sexit 1"
},
You need to specify the command to run when executing npm run. in this case node
"scripts": {
"dev": "node lite-server",
"test": "echo \"Error: no test specified\" && sexit 1"
}
You can read more about the scripts property here.
I am trying to do basic CLI with node, but seems like it is not linking from/to correct directory.
I have file named create, which contains command: console.log(process.cwd());
When I run in bash node create, it gives me that outcome:
/Users/katya/Desktop/code/drawer.
However, after running npm link (or sudo npm link), it prints that:
/Users/katya/.npm-packages/bin/dosmth -> /Users/katya/.npm-packages/lib/node_modules/drawer/create
/Users/katya/.npm-packages/lib/node_modules/drawer -> /Users/katya/Desktop/code/drawer
and after that if I run dosmth in bash I get:
bash: dosmth: command not found
I assume there is something to do with ./npm-packages/ appearing in the path.
I tried to delete node completely from computer and install again but did not help.
If you have any idea I would really appreciate your help.
My package.json:
{
"name": "drawer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"dosmth": "create"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Have you tried changing bin to point to your index.js?
{
// Other keys ...
"bin": {
"dosmth": "./index.js"
},
//...
}
I have an application which uses esm modules and import statements. It runs fine in development using the --experimental-modules flag with nodemon, but when I run it in production using forever the imports fail because I don't know where to put the flag. When I run npm run build with the package.json as below it says sh: --experimental-modules: command not found.
If I add --experimental-modules to the "script" part of forever-config.json it says the same thing.
I've searched the internet several times. How do use the --experimental-modules flag with forever? I'm running Node v10.16.0.
{
"type": "module",
"name": "application_name",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "concurrently \"npm run build:dev\" \"npm run start-server:dev\"",
"build": "npm run build:prod && npm run start-server:prod",
"build:dev": "webpack --config webpack-dev.config.js",
"build:prod": "webpack -p --config webpack-prod.config.js",
"start-server:dev": "NODE_ENV=development nodemon --config nodemon.json --experimental-modules src/server/index.mjs ",
"start-server:prod": "NODE_ENV=production --experimental-modules forever -o ./forever-out.log -e ./forever-error.log start forever-config.json"
},
//rest of package.json left out for brevity
}
forever-config.json
{
"uid": "application_name",
"max": 5,
"spinSleepTime": 1000,
"minUptime": 1000,
"append": true,
"watch": false,
"script": "src/server/index.mjs"
}
I seem to been able to make it work using the command attribute where I pass this flag, e.g:
{
"uid": "myApp",
"max": 100,
"spinSleepTime": 30000,
"append": true,
"watch": false,
"command": "node --experimental-modules",
"script": "./scripts/some-script.mjs",
"path": "D:/MyProject",
"workingDir": "./",
"sourceDir": "./",
"logFile": "./forever-script-log.log",
"outFile": "./forever-script-out.log",
"errFile": "./forever-script-err.log",
"args": ["--someOpt", "someVal"]
}
Note: you can also use args to pass args to your script/app.
In case you have no access to forever-config.json, you can run the command like this:
forever -c "node --experimental-modules" start app.js
You can also set the NODE_OPTIONS environment variable in .bashrc or the like:
export NODE_OPTIONS="--experimental-modules"
(export seems to be required when using NVM)
*package.json**
{
"name": "test-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node build build_name"
},
"author": "",
"license": "ISC",
"devDependencies": {
}
}
build.js
console.log("Building Code. Output File Name:", process.env.build_name);
Command Line
$ npm run build build_name="web"
I want to pass a parameter build_name from the command line while executing build script. I will use that param inside my build script. Can someone tell me how to achieve this? Also if I did not pass build_name from the command line, can we send a default value from here "build": "node build build_name" instead of build_name.
Use the "yargs" module.
Step 1:
npm install yargs
Step 2:
vi test.js
press i and copy the below code and paste it.
'use strict';
const args = require('yargs').argv;
console.log('Name: ' + args.name);
console.log('Age: ' + args.age);
Step 3 : Execution
node test.js --name=jacob --age=45
Step 4: output
Name: jacob
Age: 45
Let me know if it helps.
I have a package.json file that looks like this:
{
"name": "myapp",
"version": "0.0.1",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "webpack-dev-server --port=4200",
"build": "webpack",
"docker-build": "docker build -t myapp .",
"docker-run": "docker run -d --name myapp -p 9090:9090 myapp",
"docker-push": "docker build -t myapp . & docker tag myapp myrepo/myapp:{$VERSION} & docker push myrepo/myapp:{$VERSION}"
},
...
}
Im trying to get the version number when I run npm run docker-push so it will automatically tag the release with the version number of the package.json. How can I achieve this?
You can use the environment variable npm_package_version.
Tip: To see all available environment variables set by npm you can add "printenv | grep npm" as a script:
{
"name": "myapp",
"version": "1.0.0",
"scripts": {
"start": "node app.js",
"version": "echo ${npm_package_version}", // return current version
"vars": "printenv | grep npm" // return all ENV vars with 'npm'
}
// ...
}