5

I a trying to use npm to minify javascript.

This is my package.json:

{
  "name": "name1",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "minifyjs": "minifyJs",
    "minifycss": "minifyCss",
    "minifyhtml": "minifyHtml"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-css": "^3.4.19",
    "html-minifier": "^3.0.2",
    "uglify-js": "^2.7.0"
  }
}

and my minifyJs script is :

var uglifyJS = require('uglify-js');
var fs = require('fs');

var result = uglifyJS.minify(['src/main1.js', 'src/main2.js'], {
    compress: {
        drop_console: true,
        unused: true
    }
});
fs.writeFileSync('dist/minifyJs.js', result.code);

When I call npm run minifyjs I get the following error:

enter image description here

What am I doing wrong - btw this was working on another machine.....

Can anyone help?

1 Answer 1

8

The entries under scripts are commands that are run by NPM. They are not simply paths to JavaScript files.

You need to tell NPM to run your JavaScript tasks using node:

...
"scripts": {
  "minifyjs": "node minifyJs",
  "minifycss": "node minifyCss",
  "minifyhtml": "node minifyHtml"
},
...
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick. Many thanks!! I must have spent 2 hours on this reinistalling and what not....Much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.