ChatGPT解决这个技术问题 Extra ChatGPT

运行 npm start 时启动脚本丢失错误

我在尝试使用 npm start 命令调试我的节点应用程序时收到此错误。

npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v0.12.7
npm ERR! npm  v2.11.3

npm ERR! missing script: start
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\andrmoll.NORTHAMERICA\Documents\GitHub\SVIChallenge\npm-debug.log
Error: missing script: start
       at run (C:\Program Files\nodejs\node_modules\npm\lib\run-script.js:142:19)
       at C:\Program Files\nodejs\node_modules\npm\lib\run-script.js:58:5
       at C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:345:5
       at checkBinReferences_ (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:309:45)
       at final (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:343:3)
       at then (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:113:5)
       at C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:300:12
       at evalmachine.<anonymous>:334:14
       at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:102:5
       at FSReqWrap.oncomplete (evalmachine.<anonymous>:95:15)
您的 package.json 文件中是否定义了 start 脚本?

R
Robbie

看起来您可能没有在 package.json 文件中定义 start 脚本,或者您的项目不包含 server.js 文件。

如果你的包根目录中有 server.js 文件,那么 npm 将默认启动命令为 node server.js。

https://docs.npmjs.com/misc/scripts#default-values

您可以将应用程序脚本的名称更改为 server.js 或将以下内容添加到您的 package.json

"scripts": {
    "start": "node your-script.js"
}

或者...您可以直接运行 node your-script.js


我很难理解,当您拥有 main 时,还需要 start 吗?
@Val main 是您使用 require('your-module') 将模块包含在另一个项目 - docs.npmjs.com/files/package.json#main 中时的入口点。 scripts.start 是您键入 npm start 时 npm 运行的脚本。
不清楚为什么 npm init 不询问启动脚本。或者至少不使用 main 作为启动脚本。
无需再做任何事情,只需添加“脚本”:{“prestart”:“npm install”,“start”:“http-server -a localhost -p 8000 -c-1”},
在我的情况下它工作正常.. "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "parcel index.html", "build": "parcel构建 --public-url .index.html" },
S
Sourabh SInha

我在第一次安装 react-js 时遇到了这个问题:这些行帮助我解决了这个问题:

npm rm -g create-react-app
npm install -g create-react-app
npx create-react-app my-app

来源:https://stackoverflow.com/a/59260128/11652661


以上对我有用;请注意像我这样的新用户命令 npm rm 将删除 create-react-app
P
Plamen Petkov

如果您在 package.json 文件中添加了第二个“scripts”键,也会发生此错误。如果你只在 package.json 中留下一个“scripts”键,错误就会消失。


这个答案可能不是一个准确的回答,但它确实帮助我找到了 npm start 不起作用的原因。
这实际上是一个准确的响应。如果您的 package.json 文件包含两个脚本条目,它将选择第二个并忽略您在第一个中记录的任何内容。您将收到问题中的错误消息。
@Plamen Petkov,您的意思是 scripts 而不是 script? (缺少 s?)
这正是我的情况,这就是为什么它忽略了我的第一个脚本键。谢谢!
T
Taher

在关闭“}”之前将其添加到 package.json 文件中

,"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
}

index.js 是一个实际的文件吗?还是我们需要创造的东西?
我记得@tomsmithweb - 这是我现有的文件。
D
Don H

我只是偶然发现了这个问题。我重新安装了 NPM,创建了一个新的 React 应用程序(基本上是一个全新的安装),但仍然没有运气。终于想通了:

我的终端不在正确的位置。

我不得不在我的应用程序中更深入地更改目录。所以我的终端在我的“项目”文件夹中,而不是我的“我的应用程序”文件夹中

路径:'/Documents/projects/my-app'


我在 Windows 终端中单击了 Duplicate tab,但没有验证位置。显然,正如您所描述的,它在上一层打开了它。谢谢!
A
Ali

您可能有导致问题的旧(全局)安装 npm。截至 12 月 19 日,npm 不支持全局安装。

首先,使用以下命令卸载软件包:
npm uninstall -g create-react-app

一些 osx/Linux 用户可能还需要使用以下方法删除旧的 npm:
rm -rf /usr/local/bin/create-react-app

这是现在唯一支持的生成项目的方法:
npx create-react-app my-app

最后你可以运行:
npm start


s
slfan

请在 package.json 中的脚本对象中使用以下代码行

"scripts": {
    "start": "webpack-dev-server --hot"
}

对我来说,它工作得很好。


没有为我工作。您还在使用 $ npm run serve 吗?
L
LaCodeM

希望它可以帮助某人。如果您没有从项目的根目录打开项目,也会发生此错误。在 VS 代码中打开它之前,请确保 cd 进入该文件夹。


G
GHULAM NABI

https://i.stack.imgur.com/TXCFe.png


S
Srinivasan N

尝试以下步骤:

npm rm -g create-react-app npm install -g create-react-app npx create-react-app my-app

这绝对有效!


c
cherry

我在第一次安装 react-js 时遇到了类似的问题:这些行帮助我解决了这个问题:

npm uninstall -g create-react-app
npm rm -g create-react-app
npm install -g create-react-app
npx create-react-app my-app

这在我的情况下有效。


使用最新的 npm install 来安装 create-react-app,你不应该使用 '-g' 标志
K
KARTHIKEYAN.A

检查具有“脚本”属性的 package.json 文件是否存在。如果不更新这样的脚本属性

{
  "name": "csvjson",
  "version": "1.0.0",
  "description": "upload csv to json and insert it into MongoDB for a single colletion",
  "scripts": {
    "start": "node csvjson.js"
  },
  "dependencies": {
    "csvjson": "^4.3.4",
    "fs": "^0.0.1-security",
    "mongodb": "^2.2.31"
  },
  "devDependencies": {},
  "repository": {
    "type": "git",
    "url": "git+https://github.com/giturl.git"
  },
  "keywords": [
    "csv",
    "json",
    "mongodb",
    "nodejs",
    "collection",
    "import",
    "export"
  ],
  "author": "karthikeyan.a",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/homepage/issues"
  },
  "homepage": "https://github.com/homepage#readme"
}

S
SagarPPanchal

如果您使用 babelify 和 watchify,请转到:

包.json

并将其添加到“脚本”中:

"scripts": {
    "start": "watchify the-path-to-your-source-jsx-file -v -t [ babelify --presets [ react ] ] -o the-path-to-your-output-js-file"
}

一个例子是:

"scripts": {
    "start": "watchify src/main.jsx -v -t [ babelify --presets [ react ] ] -o public/js/main.js"
}

感谢 DevSlopes 的 Mark Price


Y
Yream

就我而言,如果是react项目,可以尝试升级npm,然后升级react-cli

npm -g install npm@version
npm install -g create-react-app

L
LinusGeffarth

根据反应文档https://create-react-app.dev/docs/getting-started/,以下命令将解决该问题。

npx create-react-app my-app
cd my-app
npm start

B
Basilisk Wise

现在不鼓励全局安装 create-react-app。而是通过执行以下操作卸载全局安装的 create-react-app 包: npm uninstall -g create-react-app (如果此命令不适合您,您可能必须手动删除包文件夹。一些用户报告他们必须删除文件夹手动)

然后你可以运行 npx create-react-app my-app 再次创建反应应用程序。

参考:https://github.com/facebook/create-react-app/issues/8086


D
Deva

应避免使用不稳定的 npm 版本。

我观察到一件事是基于 npm 版本的问题,npm 版本 4.6.1 是稳定版本,但 5.x 不稳定,因为如果它是稳定版本,则在使用默认模板创建时 package.json 将被完美配置,因此我们手动不这样做'不需要添加该脚本。

我在 npm 5 上遇到了以下问题,所以我降级到 npm 4.6.1 然后它对我有用,

错误:尚不支持 npm 5

看起来您正在使用最近发布的 npm 5。

不幸的是,Create React Native App 还不能与 npm 5 一起使用。我们建议使用 npm 4 或 yarn,直到一些错误得到解决。

您可以在以下位置关注 npm 5 的已知问题:https://github.com/npm/npm/issues/16991

Devas-MacBook-Air:SampleTestApp deva$ npm start npm ERR!缺少脚本:开始


R
RobC

另一个可能的原因:当您的项目在 yarn 中初始化时,您正在使用 npm。 (我自己做了这个)。所以它将是 yarn start 而不是 npm start


f
furkanayilmaz

我遇到了同样的问题。我试图在 VS 代码终端启动它。所以我在我的电脑终端(不在 VS Code 中)启动开发环境。有效。在启动之前确保您在终端的文件中


G
Goodlife

确保端口开启

var app = express();
app.set('port', (process.env.PORT || 5000));

BLAL BLA BLA 最后你有这个

app.listen(app.get('port'), function() {
    console.log("Node app is running at localhost:" + app.get('port'))
});

仍然是节点 js 中的新手,但这导致了更多。


V
Viktor Poliushko

看看你的 client/package.json。你必须有这些脚本

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject"
}

t
thatguy1155

我收到此错误是因为我不在终端的正确目录中。

带有脚本的应用程序位于文件夹 B 中。文件夹 B 位于文件夹 A 中。我在 vscode 中打开文件夹 A 并在内置终端中输入“npm run start”并得到错误。尝试“cd 文件夹 B”,在 ide 中打开文件夹 B,或者比我一开始更好地组织你的东西。


J
Juliano Soder

当我尝试运行“npm run start”时,我遇到了同样的错误

我的项目应该以“npm run serve”开始

https://i.stack.imgur.com/aEgXg.png

因此,请始终确保使用正确的命令运行它,在我的情况下是

npm run serve

您的克隆项目应该有一个类似“serve”的脚本名称。否则这将不起作用。启动反应项目的默认脚本名称是“start”。但最重要的是您可以将其更改为您想要的任何名称。查看克隆项目的“package.json”文件。在其中,在“script”属性中,您应该会看到类似“serve”:“react-scripts start”或“serve:”:“node index.js”的内容。所以我认为,现在您可以了解您的项目中发生了什么。
R
RobC
"scripts": {
  "prestart": "npm install",
  "start": "http-server -a localhost -p 8000 -c-1"
}

根据您自己的配置,将此代码段添加到您的 package.json 中。


M
Mohamed Salah

我有同样的问题。我尝试在 package.json 文件中编写代码,如下所示

    "scripts": {
    "start": "<your-script-file>.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

E
Eman Jayme

我已经解决了我的。它不是与代理行为有关的 NPM 错误。

如果你在代理后面,

MAC
 1.  Goto System Preference (gears icon on your mac)
 2.  click your network
 3.  click advanced
 4.  click proxy
 5.  check excludes simple hostnames
 6.  add this line below (Bypass Proxy Settings...) "localhost, localhost:8080"

refer to the npm echo: "Project is running at http://localhost:8080/"

Windows
 1.  Goto your browser Proxy Settings (google it)
 2.  check Bypass local address
 3.  add this line below "localhost, localhost:8080"

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅