Teleogrid's avatar

Error trying to run "npm run prod" from within a shell script

Hello,

I'm running npm run prod from within a docker container. If I run it manually on the command line tool, everything woks fine.

But when I run the same command from within a shell script, it fails.

> prod
> npm run websocket-prod && npm run production

> websocket-prod
> node prepare-build.js && cross-env process.env.target=server process.env.source_map=no NODE_ENV=production node_modules/webpack/bin/webpack.js --config=node_modules/laravel-mix/setup/webpack.config.js

node:events:498
      throw er; // Unhandled 'error' event
      ^

Error: spawn node_modules/webpack/bin/webpack.js ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
    at onErrorNT (node:internal/child_process:478:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ChildProcess instance at:
    at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
    at onErrorNT (node:internal/child_process:478:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn node_modules/webpack/bin/webpack.js',
  path: 'node_modules/webpack/bin/webpack.js',
  spawnargs: [ '--config=node_modules/laravel-mix/setup/webpack.config.js' ]
}

Before that I had an error with missing cross-env which I was able to resolve by intalling cross-env gloablly within the docker container. Now I am stuck with the above error.

0 likes
11 replies
Nakov's avatar

You can try and remove the node_modules folder using rm -rf node_modules and let the script install all the packages. Because it might be permissions issue.

Teleogrid's avatar

@Nakov The script does actually also do all the installation beforehand. So that should be OK.

#!/bin/bash

# Exit on error
set -e
set -o pipefail

[...]

cd /home/dist

git fetch --all
git checkout "$BUILD_BRANCH" -f
git reset --hard origin/$BUILD_BRANCH
git clean -f -d

[...]

# Update vendor files
composer install --optimize-autoloader --no-dev

php artisan config:cache
php artisan route:cache
php artisan view:cache

# cross-env wasn't found until it was installed globally
npm install cross-env -g

npm install --production
npm run prod

[...]
Sinnbeck's avatar

@Teleogrid can you share a minimum reproducible version of the script that has the error? Like 2 or 3 lines of code

Nakov's avatar

@Teleogrid You said you ran npm run prod before you started the script. So the node_modules is already there, but with different permissions is the point I am trying to make.

Or are you completely destroying the container with the filesystem and then rebuilding it over again?

1 like
Teleogrid's avatar

I also added rm -rf ./node_modules to the script just to make sure. But the error stays the same.

Sinnbeck's avatar

@Teleogrid what if you just do the absolute minimum?

#!/bin/bash
npm install --production
npm run prod

Also I notice you install npm for production. Are you certain that you don't have any needed dependencies in dev Dependencies?

Teleogrid's avatar

@Nakov I'm completly rebuilding ... but I discovered that now it also does not work if I run it manually anymore ... I'm just checking what I did differently ...

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Teleogrid try installing all npm packages

#!/bin/bash
npm install
npm run prod 
1 like
Teleogrid's avatar

@Sinnbeck

Great, that was it! Thanks for the suggestion.

So as I do not want to have non-production packages on the live server, I now decided to first install all packages in order to build javascript bundles and then do a npm prune --production before pushing it to the server.

# install all packages so bundles can be built
npm install

npm run prod

# remove unnecessary dev packages from production build
npm prune --production
Sinnbeck's avatar

@Teleogrid you can also delete the node modules directory. It isn't needed after you have compiled

Teleogrid's avatar

@Sinnbeck Ah yes, true. But I also have a node server running in the same project using server side imports. I'm not sure, but I belief those imports are using node_modules directory. I have to check.

Please or to participate in this conversation.