Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lewolf's avatar

node_modules as a symlink, webpack fails (Cannot find module webpack.mix) in Laravel 5.4

Hi there, I'm using Laravel 5.4.15 and my node_modules folder is outside project root. I have trouble migrating from elixir to mix.

The physical path is:

/var/www/vhosts/example.com/persistent/node_modules

Project root is:

/var/www/vhosts/example.com/releases/release_uuid_changes_all_the_time

Now in project root, I have setup a symlink to node_modules like this:

/var/www/vhosts/example.com/releases/release_uuid/node_modules => /var/www/vhosts/example.com/persistent/node_modules

This is done so I don't have to keep the huge node_modules folder in VCS, only the configuration. I'm pretty sure this is a common setup.

When after npm install I first ran npm run dev I encountered:

Error: Cannot find module '/var/www/vhosts/example.com/releases/2017-03-04_23-50-23_d1f0128/node_modules/cross-env/bin/cross-env.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:422:7)
    at startup (bootstrap_node.js:143:9)
    at bootstrap_node.js:537:3

npm ERR! Linux 3.16.0-4-amd64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "dev"
npm ERR! node v7.6.0
npm ERR! npm  v4.3.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ dev: `node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1

etc, so I had to change the "scripts" section of my package.json to this:

"scripts": {
    "dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },

Now cross-env is working fine, but when running npm run dev I hit the next error:

Error: Cannot find module '/var/www/vhosts/example.com/persistent/webpack.mix'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Mix.initialize (/var/www/vhosts/example.com/persistent/node_modules/laravel-mix/src/Mix.js:42:9)
    at Object.<anonymous> (/var/www/vhosts/example.com/persistent/node_modules/laravel-mix/setup/webpack.config.js:18:5)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at requireConfig (/var/www/vhosts/example.com/persistent/node_modules/webpack/bin/convert-argv.js:96:18)
    at /var/www/vhosts/example.com/persistent/node_modules/webpack/bin/convert-argv.js:109:17
    at Array.forEach (native)

npm ERR! Linux 3.16.0-4-amd64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "dev"
npm ERR! node v7.6.0
npm ERR! npm  v4.3.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ dev: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1

So basically it's trying to look for webpack.mix.js inside the physical location of node_modules instead of the project root, how do I tell it the other way round?

I'm kinda new to webpack and mix so need help here please!

0 likes
6 replies
Lewolf's avatar

Noteworthy, the official Laravel documentation suggests I have "two choises" for adding custom webpack configuration, the first one obviously useless in my case, and the second one it forgets to mention, but I suppose it's about copying webpack.config.js from its original location to my project root and pointing to it from package.json "scripts" section.

I've done that, but I don't see how I can override the path to webpack.mix.js inside webpack.config.js

Lewolf's avatar

Looks like I found the solution (at least for my case).

1/ As mentioned in the thread, copy webpack.config.js from node_modules/laravel-mix/setup to your project root

2/ inside that copy of webpack.config.js, change this line (somewhere around line 18)

Mix.initialize();

to this:

Mix.initialize(__dirname);

3/ Make sure your new config is loaded, inside package.json change the path to config:

"scripts": {
    "dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=webpack.config.js",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=webpack.config.js",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js"
  },

Worked for me.

Yugi's avatar

Hi, do you know which line to change right now? Cause that Mix.Initialize() line is no longer in webpack.config.js :/

Lewolf's avatar

Sorry @Yugi I don't know that... I'd probably search through laravel-mix package files for "Mix.initialize" and then figuring out how to pull that line / file out into your project to customize

Lewolf's avatar
Lewolf
OP
Best Answer
Level 4

Looks like I found the (perhaps again temporary) solution to this constantly evolving "Mix"

So when you copy webpack.config.js from laravel-mix package to your project folder, it will have in the beginning:

require('../src/index');
require(Mix.paths.mix());

Change this to:

require('./node_modules/laravel-mix/src/index');
Mix.paths.setRootPath(path.resolve(__dirname));
require(Mix.paths.mix());

don't forget that the scripts in package.json should be like this (as per the earlier solution):

"production": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js"
1 like

Please or to participate in this conversation.