简体   繁体   English

无法导入节点模块

[英]Cannot import node module

I'm trying to improve my node script that essentially converts a modified LESS file to CSS. 我正在尝试改进我的节点脚本,该脚本实际上将修改后的LESS文件转换为CSS。 It ends up making a bunch of redundant CSS so I wanted to use another module to "minify" it. 最终产生了一堆多余的CSS,所以我想使用另一个模块来“缩小”它。 I tried the official LESS one as well as what seems to be a third party but neither work. 我尝试了官方的LESS,以及似乎是第三方的,但均无济于事。

Both work via. 两者都通过。 the command line just fine, however whenever I try and import them in the script, it throws an error: 命令行就可以了,但是,每当我尝试将其导入脚本时,它都会引发错误:

module.js:338
    throw err;
          ^
Error: Cannot find module 'clean-css'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (M:\Sped\Custom CSS\compile.js:12:16)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

I've attempted to install them multiple times both with and without the -g flag (not entirely sure what that even does). 我尝试使用-g标志(和不使用-g标志)多次安装它们(不完全确定该怎么做)。

I've found other questions like this but they seem to be the other way around in that they can import them in the script but no command line use, and they were on Mac or Linux. 我发现了类似的其他问题,但是它们似乎是相反的,因为它们可以将它们导入脚本中,但不使用命令行,并且它们在Mac或Linux上。

Not sure if it makes much difference but I'm on Windows 10 64-bit. 不知道是否有很大的不同,但我使用的是Windows 10 64位。

Is anyone able to tell me what I can do to resolve this issue? 有谁能告诉我该如何解决这个问题?

So, to start your node project, go into it's directory, and type: 因此,要启动您的节点项目,请进入其目录,然后键入:

npm init

This will guide you through the process, and create a package.json for you. 这将指导您完成该过程,并为您创建package.json。 The package.json will store all of your project dependencies. package.json将存储所有项目依赖项。 From this point you can add your "clean-css". 此时,您可以添加“ clean-css”。 Browse for the right package here: 在此处浏览正确的软件包:

https://www.npmjs.com/search?q=clean-css https://www.npmjs.com/search?q=clean-css

If you wanted the wintersmith-clean-css installed locally (to this project only), and save to package.json, you would use the following: 如果您希望将Wintersmith-clean-css安装在本地(仅此项目),并保存到package.json,则可以使用以下命令:

npm install wintersmith-clean-css --save

This should get you up and running now. 现在应该可以开始运行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM