78

I have installed webpack (Symfony encore) using npm as follows:

sudo npm install -g @symfony/webpack-encore --save-dev

I ran this from /var/www/project

I was required to install globally due to issues with package managers and shared folders when dealing with Windows host / Linux guest.

I cannot install webpack (node_modules) in the same directory (or under it) as the /var/www/project

So my package.json file looks like this:

{
  "name": "test",
    "version": "1.0.0",
  "description": "This is a test",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {
    "@symfony/webpack-encore": "^0.15.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Alex",
    "license": "MIT"
}

I run encore from /var/www/project using this

/usr/local/bin/encore dev

I get this lovely output in return:

Running webpack ...

  TypeError: Cannot read property 'match' of undefined

  - index.js:125 parse
    [lib]/[webpack-encore]/[yargs-parser]/index.js:125:12

  - index.js:761 Function.Parser.detailed
    [lib]/[webpack-encore]/[yargs-parser]/index.js:761:10

  - yargs.js:938 Object.Yargs.self._parseArgs
    [lib]/[webpack-encore]/[yargs]/yargs.js:938:27

  - yargs.js:927 Object.get [as argv]
    [lib]/[webpack-encore]/[yargs]/yargs.js:927:19

  - index.js:725 Object.configureRuntimeEnvironment
    [lib]/[@symfony]/webpack-encore/index.js:725:54

  - index.js:770 Proxy.parameters
    [lib]/[@symfony]/webpack-encore/index.js:770:45

  - webpack.config.js:3 Object.<anonymous>
    /var/www/project/webpack.config.js:3:8

  - module.js:573 Module._compile
    module.js:573:30

  - module.js:584 Object.Module._extensions..js
    module.js:584:10

  - module.js:507 Module.load
    module.js:507:32

What am I missing?

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68

16 Answers16

150

Simple answer just run

rm -rf package-lock.json
Ashh
  • 44,693
  • 14
  • 105
  • 132
David S Lee
  • 1,909
  • 1
  • 10
  • 13
  • 31
    No, it should not. This is "the recommended workaround" but is incredibly broken in practice. Having a locked dependency in `package.json` in no way ensures *that* dependency's dependencies will remain consistent, so removing the `package-lock.json` file, `npm install`ing, and generating a new*lock file *will allow* those sub-dependencies to change, which often completely breaks any future building. Using *an existing lock file* is often the only way to maintain builds (that is the POINT of the file, after all) so removing it completely goes against the reason for its existence. – kevlarr Jan 29 '19 at 19:00
  • Im really impressed by this straight short answer – PYK Aug 20 '19 at 04:15
  • 1
    simple comment : what if I want to have it because package-lock.json has the precise version of package I know ARE working unlike package.json which don't provide specific version. – Ilan Schemoul Aug 29 '19 at 12:30
  • 1
    great answer. Thanks. Although after reading ans I just deleted package-lock.json manually :) – pramodpxi Jan 02 '20 at 12:54
  • Solved my issue, but in a production project where the versions of the packages you are using are important it is not a solution, can not think of a better solution myself. – Jeremy Jan 02 '21 at 11:52
  • 2
    As @kevlarr said, this isn't a valid solution when you rely on the lock file (e.g. when using npm ci you have to have a lock file) – Sam Lahm Jan 19 '21 at 21:30
  • why is the -r option used? Isn't that for directories? – Cave Johnson Apr 09 '21 at 17:54
  • Is `-rf` really necessary? – Jacob Lockard Aug 14 '21 at 22:59
  • Actually, depending on it. Most of cases are no needed -rf – David S Lee Aug 16 '21 at 01:28
31

Try to delete package-lock.json and node_modules folder and after that run npm install or yarn install

Muho
  • 3,188
  • 23
  • 34
13

Simply remove package-lock.json with this cmd command:

rm -rf package-lock.json
TechWisdom
  • 3,960
  • 4
  • 33
  • 40
Muhammad Usman
  • 155
  • 1
  • 2
  • This just duplicates another answer - which is also not one that will work universally. – mgol May 24 '23 at 09:39
12

An additional reason for this message can be a mismatch between the npm major version that the package-lock.json was generated with and the version the npm install is run on.

If for example the lockfile was generated using npm@5 and later you try to npm install on npm@6 because of lets say a minor node@8 update you will see this error.

The solution here is like mentioned in the other answers to remove node_modules and package-lock.json and npm install on npm@6 again. Or stay on npm@5 for lockfile and later install.

crackmigg
  • 5,571
  • 2
  • 30
  • 40
6

You need to remove package-lock.json from the root directory of the application. After this, you can update your new package.

Vishal Sinha
  • 77
  • 1
  • 4
3

Try the below:

  1. Remove or delete both the 'node_modules' folder and 'package-lock.json' file.
  2. Run npm install in CMD or Terminal inside the project directory.

The issue should be fixed.

2

Try npm cache clean --force

This will clear the npm cache and may resolve your issue.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
  • 2
    Thank you for this snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – jasie Mar 02 '21 at 12:09
1

Delete package-lock.json file and retry installing by command - npm install.

Pankaj Chauhan
  • 346
  • 3
  • 8
0

Installing webpack-encore globally is currently not supported. The error message however is not really explanatory.

There is an issue open to improve this error message: https://github.com/symfony/webpack-encore/issues/36

You should try to install webpack-encore locally, this is the real issue here.

Kristof Feys
  • 1,822
  • 1
  • 19
  • 36
  • I kinda figured this out - unfortunately webpack doesn't seem to like being installed "locally" because of the shared folder (symlink) issues when using Windows hosts and Linux guests. I had a similar problem with PHP Composer package manager and finally solved it by changing the location of the binaries. I had similar success (for one day) with webpack and NPM using the --no-symlink option. But that has since stopped working. – Alex.Barylski Sep 28 '17 at 18:43
0

After adding an argument to the configureRuntimeEnvironment method it started working for me:

Encore.configureRuntimeEnvironment('dev');

Somehow if you omit the argument(s) the error keeps showing up.

whitebrow
  • 2,015
  • 21
  • 24
0

I have this same issue since Java 11. Try to run yarn install ... instead. It works for me.

AchillesVan
  • 4,156
  • 3
  • 35
  • 47
0

If you are trying to npm install with a lock file, you might actually be looking for the npm ci command which installs the version-locked dependencies.

npm-ci documentation: https://docs.npmjs.com/cli/ci.html

Eddie Hong
  • 88
  • 9
0

If you're having trouble with NPM, use YARN. (Especially Windows users)

But you MUST first:

  1. Delete the node_modules folder before switching over
  2. Use Git bash as your default editor if you're using - I have found that on my system it just plays a little less rough
  3. Ensure that you have followed this guide to set up SSH key on your machine with Github (https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent). Don't skip this, otherwise packages will not download correctly

Also, you might want to think of using Laragon to have a standardized development environment instead of having to ensure you have installed WAMP and all sort of goodies by yourself.

I've pulled my hair out for ages trying to figure this out, wondering why sometimes things just weren't working.

I hope this helps someone out there.

TheGasMan
  • 17
  • 2
  • 1
    Please also read the logs to ensure that there are no missing dependencies when you try running "yarn install", and add them manually by using "yarn add PACKAGE-NAME" – TheGasMan May 23 '20 at 07:24
0

Try rebuilding node-sass inside your project directory

npm rebuild node-sass

Learn More
  • 937
  • 1
  • 8
  • 8
0

If your having the following error when running: npm install

npm ERR! Cannot read property 'matches' of null

Then do the following delete on Windows: C:\Users/{User-Name}\AppData\Roaming\npm C:\Users/{User-Name}\AppData\Roaming\npm-cache

Then re-run: npm install

Worked for me enjoy. :)

Nico
  • 58
  • 7
0

Follow the steps below:

npm cache clear --force(after execution, NPM install is ineffective, and then proceed to step 2) Delete package-lock.json under the project folder, and then run NPM install (step 3 is ineffective)

rm -rf node_modules
rm package-lock.json
npm cache clear --force
npm install
Ali Torki
  • 1,929
  • 16
  • 26