How can I solve this errorv - npm start - npm ERR! missing script: start - node.js

I am receiving this error when I tried to write npm start command after installing the react.

Check your package.json file. It should contain the start entry in the scripts key.
Read the npm docs for detailed information.
Your package.json should contain this entry:
"scripts": {
"start": "node main.js"
}
Replace main.js with the script name you are trying to run. You can test its execution by just running node main.js in the console and determining whether that even works.

Related

how does cross-env command works in nodejs?

I have the following line in my package.json
"scripts": {
"start": "cross-env NODE_ENV=development node index.js"
}
I can see that "yarn start" command is running fine, but when I run
"cross-env NODE_ENV=development node index.js" command directly in the terminal, I am getting the following error:
zsh: command not found: cross-env
If cross-env is not registered in the terminal, how does "yarn start" command works?
https://docs.npmjs.com/cli/v7/configuring-npm/folders#executables
When in local mode, executables are linked into ./node_modules/.bin so that they can be made available to scripts run through npm. (For example, so that a test runner will be in the path when you run npm test.)
It's simply a feature to make things easier. It also means if you're working a project with multiple people, you only have to npm install --save a module--you don't have to worry about everyone in your project manually installing it globally. If you wish to run it yourself on the command line, you can either:
Install the module globally
Manually type in the command line ./node_modules/.bin/cross-env

Getting error while running npm start after installing react JS

Error while running npm start.Getting this error while running npm start command
This problem can occur from 3 things:
You don't have a start script in your package.json
Your project does not contain a server.js file
You added a second script key in the package.json file
If you want to use ' npm start ' to run your code, your packaje.json file must contain start script. Something like this.
"scripts": {
"start": "node app"
}

create-react-app + npm start - 'react-scripts' is not recognized as an internal or external command

Similar questions that I have tried following
"npm-run-all" is not recognized as an internal or external command
"'react-scripts' is not recognized as an internal or external command"
I am trying to create a React project via the create-react-app cli, but after creating the project I get the error
'react-scripts' is not recognized as an internal or external command, operable program or batch file"
when I try to launch the dev server via npm start.
What I have tried:
1) Make sure Node and npm are installed and up to date. From the project directory I run (via powershell)
> npm -v
6.7.0
> node -v
v11.11.0
2) Ensure that 'react-scripts' is listed with the correct version number in package.json
"name": "clipd2",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-scripts": "^2.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}...
3) Delete node_modules folder and package-lock.json and reinstall npm packages
rm -r -fo node_modules
rm package-lock.json
npm install
npm install -S react-scripts
After instalation the react-scripts directory is found (and populated) in node_modules
4) Make sure npm is in the environment PATH variable
> echo $Env:Path
....C:\Users\notMyUsername\AppData\Roaming\npm
I am at a loss for next steps. Strangely enough, I have another React app housed in the same parent directory
clipdReact
clipd
clipd2
And there are no problems when using npm start in the clipd project (whereas the failing project is clipd2)
Any suggestions or tips would be greatly appreciated!
**UPDATE
This bug was filed but is still open
react-scripts is not recognized
You can start the dev server (from your project's directory) with
.\node_modules\.bin\react-scripts start
Be careful generating the production build with a command similar to the one above - I had problems with babel and polyfills when trying to do so.
What worked for me in the end, was setting the script shell to powershell. The command for that is npm config set script-shell powershell and npm config delete script-shell to reset the config. I'm not sure why that worked, my guess is that since there are always 3 script files in node_modules\.bin that somehow the wrong shell was used for the wrong script or something like that.
npm install react-scripts --save --force

Silencing errors on failures for npm run-script

When you run npm test and it fails, you get the test outputs + a single error message, like so:
npm ERR! Test failed. See above for more details.
However, I made a custom script called lint, like so:
// package.json
{
// ...
"scripts": {
// ... definition for test ...
"lint": "./node_modules/jsxhint/cli.js src/",
}
}
Alright, simple enough. But when you run npm run lint and it fails, Rather than the nice looking error for npm test, you get a massive error message after the output of the linter:
npm ERR! Darwin 14.0.0
npm ERR! argv "node" "/usr/local/bin/npm" "run-script" "lint"
npm ERR! node v0.10.32
npm ERR! npm v2.1.7
npm ERR! code ELIFECYCLE
# and ~15 more lines...
Is there a way to silence all this junk so I can have a clean output like the npm test script? I see how they caught the error in the npm source code, but I don't think I can just add a custom command without forking npm like that... Hope I'm wrong!
But if I am, would I be better off just pushing off a task like this to a tool like Grunt? Thanks!
Use the npm run --silent option:
$ npm run --silent test
Even less typing if you define a shell alias:
$ alias run='npm run --silent'
$ run test
If you don't care about preserving the return code of the linter process, you can always configure your package.json like this:
{
// ...
"scripts": {
// ...
"lint": "eslint . || true",
}
}
I've just been trying to figure out the same. Not a perfect answer but it kind of worked to specify linting as a pretest script (docs) like so:
// package.json
{
// ...
"scripts": {
// ... definition for test ...
"pretest": "./node_modules/jsxhint/cli.js src/",
}
}
Then, when you type in npm test the first time, you will only get a single-line error from NPM. Obviously, that means you won't be able to run your tests if you haven't linted.
The other option is to use some kind of third party task runner like Make, Grunt or Gulp.
I've only used Make, and I think it's the most painless to set up (at least on OSX and Linux, not sure about Windows).
Create a Makefile in your root that looks like so:
lint:
./node_modules/.bin/jslint ./*.js # or whatever your lint command is
test:
./node_modules/.bin/mocha test/*.js # or whatever your test command is
.PHONY: lint test
Then type make test and make lint to run those commands.
You can silence the errors by redirecting the stderr to /dev/null. For example:
{
"test": "karma start" (package.json)
}
running:
$ npm run test 2> /dev/null
will now send all npm errors to /dev/null but normal input will still be visible in the console.
Because the error is thrown by npm, after karma exiting with a non-zero status, doing the following is not enough:
{
"test": "karma start 2> /dev/null"
}
but you can overcome it by creating another task that calls that task with stderr redirection:
{
"test": "karma start",
"test:silent": "npm run test 2> /dev/null"
}
this will ensure that the npm error messages are hidden

How does "npm" run "npm test"?

I always thought that npm test command just launches what I would write in package.json inside scripts: { test: ...} section. But I have this weird bug when it doesn't work.
So, I have this piece of config in package.json
"scripts": {
"start": "node index.js",
"test": "mocha tests/spec.js"
}
When I try to run tests I type npm test in terminal and had this error:
module.js:340
throw err;
^
Error: Cannot find module 'commander'
But everything is OK when I type just mocha tests/spec.js. Any ideas why is that?
UPDATE:
I've tried to install commander and I had an error Cannot find module 'glob'. After installing glob I have
Error: Cannot find module '../'**
But actually question is why do I have these errors and why is everything OK when running mocha tests/spec.js?
You may have two versions of mocha installed: one globally (npm install -g mocha) and one locally, which appears to be broken.
When you run a script through npm, either as npm run-script <name> or with a defined shortcut like npm test or npm start, your current package directory's bin directory is placed at the front of your path. For your package that's probably ./node_modules/.bin/, which contains a link to your package's mocha executable script.
You can probably fix this by removing the local mocha and reinstalling it with --save-dev:
rm -rf node_modules/mocha
npm install --save-dev mocha
That should get you a working local copy of mocha with all its dependencies (commander etc.) installed.

Resources