The scripts
property of your package.json
file supports multiple built-in scripts with their preset life cycle events and also arbitrary scripts.
Cause of Error
This npm ERR! Missing script: “start” error is encountered, when you run the npm start
or npm run start
command by missing to add start
property inside the script
property in the package.json.
Solution
To resolve this issue, add the start
property in package.json.
Let us see some of the possible reasons for this error to happen and its solution.
Include Start script in package.json (Quick Solution)
The start property runs a predefined set of commands specified inside the script
property.
Your package.json should contain this start
property inside the script
property. If you fail to add this start
property, it will throw npm ERR! missing script: “start” error.
Code
//package.json
{
"name": "TechMam",
"version": "1.0.0",
"description": "TechMam Tutorials",
"main": "index.js",
"author": "sv",
"dependencies": {
"express": "^4.18.1"
}
}
//index.js
console.log("Welcome To TechMam Tutorials!!!");
npm start
You could see the script
property along with the start
property is missed in the above package.json, and as a result, the below error is thrown,
Output
npm ERR! missing script: start
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\HP\AppData\Roaming\npm-cache\_logs\2022-07-30T13_41_34_146Z-debug.log
You can fix this error by adding valid script
and start
properties in the package.json.
Code
//package.json
{
"name": "TechMam",
"version": "1.0.0",
"description": "TechMam Tutorials",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "sv",
"dependencies": {
"express": "^4.18.1"
}
}
npm start
Output
Welcome To TechMam Tutorials!!!
Remove duplicate script
property in package.json
In package.json, if you have defined a duplicate script
property, there is a high chance for npm
to throw npm ERR! Missing script: “start” on running npm start
.
If you have added more than one script
property in the package.json file, most IDE will show a warning for “duplicate object”.
Note: When you have duplicate script
properties, npm will execute only the last script
property by overriding the previous script
properties. So commands configured in the last script
property will only be executed.
If the last script property does not contain the start
command and if you run npm start
, then npm will throw npm ERR! Missing script: “start” error.
Code
//package.json
{
"name": "TechMam",
"version": "1.0.0",
"description": "TechMam Tutorials",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"scripts": {
"dev": "gulp"
},
"author": "sv"
}
//index.js
console.log("Welcome To TechMam");
npm start
Output
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favourite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run
The above package.json file contains two script
properties, and npm will only execute the last script
property by overriding the previous script
.
As the last script
property does not contain the start
property, npm threw npm ERR! Missing script: “start” error.
So you have to include only one script
property contained with the start property and remove the duplicate script
to resolve this issue.
Code
//package.json
{
"name": "TechMam",
"version": "1.0.0",
"description": "TechMam Tutorials",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "gulp"
},
"author": "sv"
}
//index.js
console.log("Welcome To TechMam!!!");
npm start
Output
Welcome To TechMam!!!
Include Server.js as an alternate to start the script
NPM command does not always require start
script commands for beginning an application.
You can use the sever.js
file as an alternative to start
script commands.
Internally npm
will run as node server.js
when npm start
is executed and by avoiding the npm ERR! Missing script: “start” error.
Code
//package.json
{
"name": "TechMam",
"version": "1.0.0",
"description": "TechMam Tutorials",
"main": "index.js",
"author": "sv",
"dependencies": {
"express": "^4.18.1"
}
}
//server.js
console.log("Welcome To TechMam, Server.js executed!!!");
//current package structure
.
├── node_modules/
├── server.js
├── package-lock.json
└── package.json
npm start
Output
> [email protected] start
> node server.js
Welcome To TechMam, Server.js executed!!!
In the above code, package.json is not configured with start
and script
properties but executing npm start
did not throw npm ERR! Missing script: “start”.
As you see server.js
is placed in the root directory, and npm executed node server.js
.
Note:
This differs from the default node behaviour of the running file specified in the package’s main attribute when executing node
.
Run file using the node command
You can directly run a specific file instead of using the start command to run a file to avoid this error npm ERR! Missing script: “start”.
Code
//index.js
console.log("Welcome To TechMam!!!");
node index.js
Output
Welcome To TechMam!!!
Initialize package.json in your project
On working with a new project, you need to initialize the project to have package.json.
npm init -y
Output
{
"name": "tm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
You can use the above command to initialize a project by creating a package.json file.
Command parameter -y
is used to setting the default values.
So now, if you run the npm start
command, you will see npm ERR! Missing script: “start” error because default package.json configuration will not come with the start
property.
To resolve this issue, please add a valid start
property inside the script
property inside package.json.
Use the latest version of the package
If you use create-react app
, the error npm ERR! Missing script: “start” might be caused by an outdated version.
Follow the below commands to resolve this issue,
# use to uninstall old create-react-app version
npm uninstall -g create-react-app
# use to clear your cache
npx clear-npx-cache
# use to create a normal React.js project
npx create-react-app my-app
# use to create TypeScript React.js project
npx create-react-app my-app --template typescript
If the above commands didn’t work, please use the use-npm
flag and create the commands above.
# use for React.js project
npx [email protected] my-app --use-npm
# use for TypeScript React.js project
npx [email protected] my-app --template typescript --use-npm
Related Topics
- How To Fix npm WARN config global –global, –local are deprecated. Use –location=global instead – Detailed Guide
- How To Solve Cannot find module error using Node.js – Definitive Guide
- How to solve npm error “npm ERR! code ELIFECYCLE” – Definitive Guide
- How To Solve Uncaught SyntaxError: Cannot use import statement outside a module when importing ECMAScript 6 – Definitive Guide