Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM Cannot read property '0' of undefined

Tags:

npm

After updated Node (upto v8.6.0) and npm (upto v5.5.1) I cannot execute command npm install. After npm install I've error message:

npm ERR! Cannot read property '0' of undefined

What's trouble or I need downgrade node/npm ?

like image 940
Dmitrii Dubrovin Avatar asked Oct 07 '17 12:10

Dmitrii Dubrovin


People also ask

Can t read Property 0 of undefined?

The "Cannot read property '0' of undefined" error occurs when trying to access the 0th index in a variable that stores an undefined value. Make sure to initialize the variable to the correct type, e.g. array or string, before accessing the index.

How do you fix undefined properties Cannot be read?

To solve the "Cannot read properties of undefined" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to access a property at a non-existent index after using the getElementsByClassName() method. Copied! const boxes = document.

What does Cannot read property of undefined mean?

What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.

Can not read property 0 of null?

The "Cannot read property '0' of null" error occurs when trying to access the 0th index on a variable that stores a null value. Before you access the index, make sure that the value allows for index access, e.g. is of type array or string.


3 Answers

I had the same problem.

I removed both node_modules and package-lock.json and then did:

npm install 

And it worked.

Edit by @OwlyMoly Due to new updates and the restriction to old dependencies in package-lock.json is causing this conflicts. By doing npm install won't fix this issue. Instead by ditching npm_modules and package-lock.json and doing npm install will load a new node_modules and that supposed to be required by package.json. You have to commit the new package-lock.json along with your latest changes of the project.

like image 195
Idan Dagan Avatar answered Oct 10 '22 20:10

Idan Dagan


Do 2 steps bellow (Window):

rm -rf ./node_modules to remove node folder

rm package-lock.json to remove package-lock.json file

then npm install to re-install the node modules

like image 37
Sakata Gintoki Avatar answered Oct 10 '22 22:10

Sakata Gintoki


Just download and install latest Yarn which is also a node package manager, developed by facebook, but has a much better dependency management. Also update your node cli (optional).

And then, install your dependencies using yarn:

yarn install

or

yarn // short version of yarn install

No errors!

You can continue to use npm after you have installed all dependencies with yarn or continue with yarn....it's your choice.

like image 4
Legends Avatar answered Oct 10 '22 22:10

Legends