Passing a param from cmd line to package.json - javascript

"scripts": {
"start": "gulp",
...
},
I'm using a package that runs via npm start. I want to pass params to the start command.
How can I pass the param in the command line?
npm start --myparam = hello
Alos, how can I access this in my package.jspn file to pass through to gulp:
"scripts": {
"start": "gulp --myparam",
...
},

If I understand your question correctly, you are trying to run gulp --myparam=hello with the npm start command.
The correct way to do this is npm start -- --myparam=hello.
Notice the -- in the command. Anything after the -- will be passed directly to the start command (gulp).
The reason for requiring the -- is that npm start (and npm run) can take parameters as well; like -s to silence the default output from npm.

Related

After I installed nodemon by "npm i nodemon" , when I tried to run it by "nodemon server.js" , it gives me this error. What should I do?

Screenshot form VS code terminalnodemon : The term 'nodemon' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the
path is correct and try again.
I just meet that question.
First, you should make sure that you had installed nodemon in a global way (npm install -g nodemon), and make sure global package dir is in the environment variables.
Secondly, you should RESTART VScode if you are opening it now.
I spent lots of time to make sure the previous one, but it still fails, then when I restart VScode, everything is fine!
Try to install nodemon globally:
https://github.com/remy/nodemon
npm install -g nodemon
and edit Your package.json for example like that:
"scripts": {
"start": "node server",
"dev": "nodemon server"
},
then in terminal enter the command =>
npm run dev
It should works for You now ;-)
Good Luck and Best regards !
Below are some solutions
This is how you can install nodemon
npm install -g nodemon
after that you have to run this command: npm run dev
here dev is (script) server name like
{
"main": "app.js",
"scripts": {
"dev": "nodemon app.js"
},
}
after that if you are facing error like
" 'nodemon' is not recognized as an internal or external command,
operable program or batch file. "
Then after install nodemon globally write below command:
npm config get prefix
in output you will get PATH and then past this path in to the Environment Variables and it solved
Restart the terminal and run this command
nodemon run dev
I hope after that it will work properly :)
I just had to use npx instead of npm
for e.g. - npx nodemon <server.js>

How do I manually change a file name in Parcel?

Every time I try to run npm start or npm build I get an error saying unknown: Entry /mnt/c/Users/kabre/Desktop/18-forkify/index.html does not exist. I got told that Parcel might be automatically renaming my index.html. Not sure how to go on about fixing this since I'm just starting out learning Parcel/npm.
Try this:
Run npm init -y in your project
Install parcel in your project: npm install parcel-bundler --save-dev
Put this in your package.json:
"scripts": {
"start": "parcel ./index.html",
}
Run npm start after this.
And will running perfect.
Find more in https://parceljs.org/getting_started.html

How to update snapshot with Jest and vue-cli

I should be able to add -u parameter when running my tests, but I can't figure out why it doesn't work:
npm run test ComponentName.spec.js -u
npm run test ComponentName.spec.js --updateSnapshot
but it doesn't work. My package.json:
"scripts": {
"test": "vue-cli-service test:unit",
I know I can just delete the snapshot files, but I'd like to figure out why the command doesn't work.
Based on the docs:
npm run test -- -u
I verified this works.
In vue-cli 3, your usual npm command calls vue-cli-service and not jest anymore. Vue-cli-service will call jest for you.
Either you can run :
npm run test:unit -- -u
the -- is so that the next arguments have to be passed to the subcommand.
Or
npx vue-cli-service test:unit -u
This will run the tests and upgrade the snapshots.
yarn test -u worked for me. We use yarn.
npm run test -- -u [file_path]
//for particular file (thumps up to echo's answer)
If you are running a project with Lerna monorepo,
You probably want to add a new script to your package's package.json file:
{
// ...
"scripts": {
// ...
"test:update:snapshot": "jest --updateSnapshot"
// ...
}
// ...
}
So you can run
npx lerna run test:update:snapshot
Or you can just enter the package and run
npm run test -- -u
Basically -- tells your command the argument -u is for its child command.

How to use node-inspector with `npm start` for my application?

I am using npm start to start my MEAN stack application, but I would like to use the node-inspector to debug some Mongoose. I know I can start the node inspector with node-inspector, but what can I substitute node --debug app.js with to make npm start work in my case?
This is my MEAN stack directory structure:
HTML views/
Angular.js public/javascript/
Express.js routes/
Node.js app.js
Mongoose js models/, connected in app.js
Mongo db connected in app.js
For more information, this is my related question.
You may want to add a seperate debug script to package.json. That way you won't have to remember to revert npm start when you're finished debugging.
"scripts": {
"start": "node ./bin/www",
"debug": "node --debug ./bin/www"
}
Start with npm run:
$ npm run debug
In package.json modify the start run command:
"scripts": {
"start": "node --debug app.js"
}
I use it like this, I also set a variable and run the inspector in one command:
npm run debug
"scripts": {
"start": "set SOAPAPI=https://example.com/&&nodemon",
"debug": "start node-inspector --web-port=8081&&set SOAPAPI=https://example.com/&&nodemon --debug"
}
*nodemon is an utility wrapper for node, you can use node instead

'var' is not defined as internal or external command in npm

I am trying to install histogramjs package. npm i histogramjs --->works well but when I try to run --> var hist = require('histogramjs')
I got the error on cmd:
'var' is not defined as internal or external command in npm
I have tried already tried
npm install var -- >get installed abd when I try to run ---> var env = require('var');
get the same error..
If run directly from node.js , get error 'cannot find module 'var'
Please help as I am very new to npm and node
Thanks
You should not type in var hist .... in your command line. What you type there, will be executed by shell, not by npm/node.
Instead, try:
Run npm init. Press enter couple of times, until it completes. This will create package.json file which is npm's configuration file. this file will tell npm, among others, which file is an entry point to your application.
Edit package.json, add "start": "node app.js", inside "scripts" element (see example below).
Create app.js in the same directory where package.json is located. Put your script in this file.
Run npm start.
If your script depends on 3rd party modules (in your case - "histogramjs" probably), install those as well:
npm install --save histogramjs
Running it without --save modifier will work as well. But --save will cause npm to put "histogramjs" as a dependency in package.json. Thanks to that, when someone gets your code later (i.e. from repository), he'll be able to simply run npm install, without even having to care which dependencies are required.
package.json
{
"name": "t",
"version": "0.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD"
}
app.js
var hist = require('histogramjs')
// ... rest of your code

Categories

Resources