1

I'm trying to improve my node script that essentially converts a modified LESS file to CSS. It ends up making a bunch of redundant CSS so I wanted to use another module to "minify" it. I tried the official LESS one as well as what seems to be a third party but neither work.

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).

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.

Not sure if it makes much difference but I'm on Windows 10 64-bit.

Is anyone able to tell me what I can do to resolve this issue?

6
  • for starters, the -g is for installing a module globally, which makes it accessible in cmd / terminal. Second: this just means it can't find the module, so go to the root of your project and tell us if there is a folder called node_modules Commented Nov 21, 2015 at 13:44
  • @CreasolDev There aren't any directories. Just a the compile.js file and the .less file. When I run node compile.js, it compiles the CSS from the LESS file creating a new file. I have 2 different require statements in the script that work fine. Commented Nov 21, 2015 at 13:56
  • If you're using modules and you've entered npm install clean-css in your CMD, it should make a node_modules folder in the folder your project is in. That's where Node.js pulls it's 'requires' from Commented Nov 21, 2015 at 13:58
  • @CreasolDev My script isn't even on the same drive as the other modules but your comment made me think. I ended up solving the problem by removing the -g flag and running it from the directory of the other modules. Commented Nov 21, 2015 at 14:10
  • So.... Does it work now? Commented Nov 21, 2015 at 14:10

1 Answer 1

2

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. The package.json will store all of your project dependencies. From this point you can add your "clean-css". Browse for the right package here:

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:

npm install wintersmith-clean-css --save

This should get you up and running now.

Sign up to request clarification or add additional context in comments.

4 Comments

Which is located where?
in the root directory of your node.js project
It's a single script used to compile a LESS file to CSS. There is no package.json file in the same directory.
If you don't want this script to have it's dependencies in a directory as described, you can always install your dependencies globally.

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.