javascript nodeJS中的uglify-js“找不到模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29300680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:21:06  来源:igfitidea点击:

uglify-js in nodeJS "Cannot find module"

javascriptnode.jsuglifyjs

提问by Arunraj

Here i am creating application to compress a javascript file

在这里,我正在创建应用程序来压缩 javascript 文件

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
  1. 在我的本地机器上安装 nodeJS
  2. 检查节点和 npm 正在工作
  3. 通过“npm install uglify-js -g”安装“uglify-js”并安装
  4. 当我尝试在命令提示符下通过“node server.js”运行时抛出错误

Error: Cannot find module 'uglify-js'

错误找不到模块“uglify-js”

Server.js

服务器.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

在中安装了“uglify-js 模块”

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

Server.js in

Server.js 中

C:\Program Files\nodejs

Can any one help?

任何人都可以帮忙吗?

回答by Connor Peet

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.

全局安装了 uglify ,它不会对您的包“可见”。您想在脚本所在的目录/包中运行npm install uglify-js(不带-g)。

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

如果您在 package.json 中跟踪依赖项(您应该这样做!),npm install --save uglify-js将自动为您添加它。