jd487
jd487

Reputation: 189

NPM start error - file path cannot find JSON package file

First time poster, thanks in advance for the help!

I'm trying to complete my first project and am running into a few issues. The main one being that when I hit npm start I get hit with this error message -

https://i.sstatic.net/gZXjV.png

I've tried hitting npm install before I run it but to no avail. Does anyone have any ideas?!

Thanks!

enter image description here

Upvotes: 1

Views: 1126

Answers (1)

Rohan Büchner
Rohan Büchner

Reputation: 5403

Without the contents of the package.json I can only guess... As some of the comments have pointed our as well, make sure your node or npm command can navigate to/see your script file


Context:

But there are 2 concepts here. node & npm

1. Node is a runtime:

In order to run a node script you'll need the node command (within the context of the project your working on, so make sure your cli is in the root of your project) like this:

node ./folder/to/your-script.js

2. NPM is a package manager.

...and it relies on having a package.json in the same directory as where you're running the npm command for most of it's commands other than init (which creates the package.json file for you)


Problem:

The second screenshot is the clue here.

You tried running npm start, where the error message is:

missing script: start

This means that your scripts block does not contain a start property

Thus it seems to me as if you are mixing the 2 concepts.

  1. If you wanted to run the script without a package.json you can use the node <filename.js> command
  2. If you have dependencies, and want to make your life a little more convenient if you have a lot of other commands and dependencies, you can use the npm run <script> command.

(Side-note: npm start is a special case that seems to be able to run or without the run keyword, which I didn't know as well until I just tested it now :P)


Solution:

Therefor to get closer to what your expecting, we can modify the package.json as per below:

You can also create custom aliases for commands within the scripts block (inside the package.json):

 "scripts": {
   "start": "node ./some-path/to/your-script.js"
   "bar": "npm run foo",
   "foo": "node ./folder/to/your-script.js"
 }

You'll notice, you can reference sibling script aliases in other script commands as well (you can also add non node commands in here, like git, etc..)

Thus with the above in your package.json you'll have access to the following commands when running npm in the same directory as the package.json.

npm start, npm run foo or npm run bar

Upvotes: 2

Related Questions