如何解决“ vue-cli-service”不被认为是内部或外部命令的问题?

当尝试运行 npm run serve时,我得到一个错误。首先我安装了 node.js,然后安装 vue 和 vue/cli。 但是当我试图运行 server as-> npm run service 时,我得到了一个错误,比如‘ vue-cli-service’不能识别为内部或外部命令。

我使用下面的安装代码:

npm install -g vue
npm install -g @vue/cli

enter image description here

有人能指导我怎样解决这个问题吗?

156089 次浏览

I think you are using cmd in windows.

Try deleting the node_modules folder and after that run npm i from the cmd.

Then try running npm run serve again and see if it works this time

In my case, the package @vue/cli-service is installed in my local node_modules environment, but not my global environment, so it cannot be used as a command. I type .\node_modules\.bin\vue-cli-service serve and it works.

As it is mentioned in terminal that node_modules is missing from your project, so you can't directly use npm run serve, first you have to do npm install then do npm run serve. It will work fine

Install vue/cli-service globally

npm install @vue/cli-service -g

This will install global npm package.

@vue/cli-service is usully installed as global, because you do not usually copy these types of packages to every project.

If the global npm package gets corrupted, it is not stored in node_modules folder, but rather in other depending on the os. Therefore removing node_modules does not help. Locations for global node_modules folders are

  • %USERPROFILE%\AppData\Roaming\npm\node_modules (Win10) or
  • /usr/local/lib/node_modules (Linux),

check this stack overflow post on how to locate global packages.

it will depend on the package manager you are using

  1. delete node_modules

  2. if you are using yarn run yarn or yarn install and then yarn serve

  3. if you are using npm run npm install and then npm run serve

In my case I ran below commands in GitBash and it worked fine

  1. npm install
  2. npm run serve

I had faced the same problem in windows. Then first I deleted the node_module. then I run npm install.

  1. If you are using cmd in windows.
  2. deleting the node_modules folder and after that run npm istall from the cmd.
  3. run npm run serve and see if it works this time

For Windows you should modify package.json to:

  "scripts": {
"serve": "vue-cli-service.cmd serve",
"build": "vue-cli-service.cmd build",
"lint": "vue-cli-service.cmd lint"
}

,

I had the same issue using windows + WSL2 (Ubuntu 20.04). Looking at the logs generated after trying to run npm i I noticed that my WSL2 environment did not have python2 installed. So to solve I ran the following commands:

  1. sudo apt-get install python2
  2. rm -rf node_modules
  3. npm i
  4. npm run serve

I faced the same in Windows. Had to run npm install again. Then it worked perfectly.

In my case, I have checked the folder of node_modules was missing. I am using Windows. So I run this in cmd.

  1. npm install
  2. npm run serve

Then I check it in localhost like usual.

This issue mostly happens when either @vue/cli is not installed or in most cases,

@vue/cli is already installed and you are currently working on a project and when running

  • yarn serve or npm run serve.

Most at times, this issue is been caused by broken dependencies. to fix this issue, simple run

  • yarn install or npm install

depending on your package manager.

Wait, what's the difference between @vue/cli and @vue/cli-service? When you install both, they show different number of packages installed. The latter solved my issue actually but everyone keeps saying install @vue/cli.

well after trying all the solutions above and it still haven't worked for you then you probably have a stupid space in the full directory of your Vue project like in my case. so remove that that space and it will work from then on.

Remember to set the NODE_ENV=development and run npm install again

I have a project, I can run it well on Linux, but i have the same issue on windows, I solve it this way (I hope in your case it works too):

  1. Delete the node_modules

  2. Install it again with npm i

Try changing the project path to one without spaces, it worked on windows 10

try running npm i or npm install and then proceed to run npm i vue after previous installation done. works for me

you need use "npm install" at Command Line

Before running "npm install", try running this command first:

npm set strict-ssl false