4

I'm doing a tutorial on VueJS. I am completely new to this, so not enitrely sure what I am doing. I followed all instructions, installed all packages, here is the check I made in terminal in VSCode:

PS C:\Users\...\Documents\Vue - Getting Started> node --version
v12.18.0
PS C:\Users\...\Documents\Vue - Getting Started> npm --version
6.14.5
PS C:\Users\...\Documents\Vue - Getting Started> vue --version
@vue/cli 4.4.1

However, whenever I try to npm run serve as it's shown in the tutorial, it shows an error stating that a package.json is missing:

npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\...\package.json'

I checked, and indeed, I don't have a file called package.json in my user folder. I only have a file called package-lock.json.

I also noticed, that on the tutorial video, the terminal has something like 1: node, while in mine, I can see 1: powershell. Here is the screenshot:

screenshot from terminal

What am I missing? Thank you


As suggested by El, I did npm init and created a package.json file in my project folder. Inside, I added the bit suggested by El. The whole package.json file now looks like this:

{
  "name": "package.json",
  "version": "1.0.0",
  "description": "package json",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "serve": "node index.js"
  },
  "author": "",
  "license": "ISC"
}

And I am now getting this error:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! package.json@1.0.0 serve: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the package.json@1.0.0 serve script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
julia2019
  • 73
  • 1
  • 1
  • 6
  • For starting any JavaScript project you need to use `npm init` for making project configurations – El. Jun 06 '20 at 16:53
  • Can you post link of the tutorial here? – dangxuanvuong98 Jun 06 '20 at 16:55
  • @El. according to the error message, NodeJS tried to find `package.json` in another folder, not the project folder. – dangxuanvuong98 Jun 06 '20 at 16:57
  • @dangxuanvuong98 "another folder" is the user root folder in Windows – El. Jun 06 '20 at 17:03
  • @El. Yes, i know, but why NodeJS try to find `package.json` in root folder. Note that, the command is called from `C:\Users\...\Documents\Vue - Getting Started`. – dangxuanvuong98 Jun 06 '20 at 17:07
  • @dangxuanvuong98 Yes, you are right. Julia please provide us the contents of your current directory by `ls` in powershell or `dir` command in CMD – El. Jun 06 '20 at 17:11
  • @dangxuanvuong98 it's just index.html. and now I also added package.json as suggested in the anwser by El – julia2019 Jun 06 '20 at 18:21

3 Answers3

6

I think there's no need to explain Nodejs ENOENT error with such a self-explanatory error message: no such file or directory which simply means index.js or any file that you want to run with npm or node isn't in the current directory. First you need to check to see if you are in the root of your project then in the root of your project you need to have a config file that is package.json for JavaScript(Nodejs, react, typescript and so on) projects. So for making that config file you can use npm init command and it will ask you predefined questions about your projects. for starting or serving your project you must have a script tag like this in your package.json file:

'scripts':{
  'serve': 'node index.js',
}

then you can use npm run serve command in the root of your project to run serve command of your package.json file.

In case of VueJS, which I am not familiar with, I think you have a index.html file in the root directory of your project and aslo as I know you have vue installed on your machine. I guess you need to replace node index.js with following command to serve your Vue application:

'scripts':{
  'serve': 'vue index.html',
}

and run npm run serve.

El.
  • 1,217
  • 1
  • 13
  • 23
  • thanks - I created a `package.json` file in my project folder. now when I try to `npm run serve` I get the following: `npm ERR! missing script: serve`. Where do I put the script you mentioned? I just have a index.html file.. – julia2019 Jun 06 '20 at 17:22
  • 1
    add it to your `package.json` file – El. Jun 06 '20 at 17:31
  • Running into another problem, updating my original question. – julia2019 Jun 06 '20 at 18:16
  • You must have a `index.js` file too. – El. Jun 06 '20 at 18:38
  • added an empty `index.js` file, now I get the following message (server doesn't start): `> package.json@1.0.0 serve C:\Users\....\Documents\Vue - Getting Started > node index.js` and nothing else happens – julia2019 Jun 06 '20 at 18:57
  • Ok, I've got it. change the `"serve": "node index.js"` to `"serve": "vue index.html"` and let me know about the result – El. Jun 07 '20 at 05:00
0

I fixed this problem with those commands:

// Locate to the folder, where the "node_modules" directory is stored

rm node_modules
npm i
Jneer
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '22 at 01:31
0

deleted the project folder and then creating vue project

vue create my-project

This is how it worked for me