uglify-js in nodeJS “Cannot find module”

可紊 提交于 2019-12-24 04:39:26

问题


Here i am creating application to compress a javascript file

Steps i made,

  1. Installed nodeJS in my local machine
  2. checked node and npm is working
  3. Installed "uglify-js" by "npm install uglify-js -g" and installed
  4. When i try to run by "node server.js" in command prompt throwing error

Error : Cannot find module 'uglify-js'

Server.js

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

var result = UglifyJS.minify('site.js', {
    mangle: true,
    compress: {
        sequences: true,
        dead_code: true,
        conditionals: true,
        booleans: true,
        unused: true,
        if_return: true,
        join_vars: true,
        drop_console: true
    }
});

fs.writeFileSync('site.min.js', result.code);

Installed 'uglify-js modules' in

C:\Users\carunraj\AppData\Roaming\npm\node_modules\uglify-js

Server.js in

C:\Program Files\nodejs

Can any one help?


回答1:


You installed uglify globally, it won't be "visible" to your package. You want to run npm install uglify-js (without the -g) inside the directory/package where your script is located.

If you're keeping track of dependencies in a package.json (you should be!), npm install --save uglify-js will automatically add it there for you.



来源:https://stackoverflow.com/questions/29300680/uglify-js-in-nodejs-cannot-find-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!