20

I'm trying to set up node-sass, following the instructions on CSS-Tricks. Node and npm are installed correctly, and the node-sass installation worked too. When I go to run node-sass --output-style compressed -o dist/css src/scss, though, I get an error message stating

'node-sass' is not recognized as an internal or external command, operable program or batch file.

I've done a fair bit of Googling and searched Stack Overflow directly. My question isn't about "node" not being recognised as a command. I know node is working as I can run node -v and npm -v, and node-sass was successfully installed after running npm install --save-dev node-sass (there's a folder in node_modules) and no errors appeared in the command line.

Other information: I am running Windows 10 and just did a clean install of node and npm before trying to use node-sass.

EDIT: I uninstalled and reinstalled with -g thanks to @Bhavik's suggestion, and it's now working

Community
  • 1
  • 1
Sam Woolerton
  • 439
  • 2
  • 8
  • 14
  • You need to do `npm install -g node-sass` to install it globally. – Bhavik Feb 28 '17 at 21:01
  • so I can't run it from the command line without installing it globally? – Sam Woolerton Feb 28 '17 at 21:02
  • 1
    It should be available in your `PATH` variable to be used. Other way can be in `package.json` you can have `scripts` where you can create `"scripts": { "node-sass": "node-sass --output-style compressed -o dist/css src/scss"` and then run it using `npm run node-sass` – Bhavik Feb 28 '17 at 21:11
  • Thanks, it's working now – Sam Woolerton Feb 28 '17 at 21:21

7 Answers7

48

You need to install it globally

npm install -g node-sass

Or add it in package.json

"devDependencies": {
    "node-sass": "4.5.0"
},
"scripts" : {
    "node-sass": "node-sass --output-style compressed -o dist/css src/scss"
}

And then do
1. npm i, which in this case would be similar to npm install --save-dev node-sass
2. npm run node-sass

Reference: npm scripts, npm-run-scripts

Bhavik
  • 4,836
  • 3
  • 30
  • 45
  • When I run `node-sass --output-style compressed -o dist/css src/scss` directly from the command line, it works, but not if I run it as a script. Is that a result of installing globally? If I want to use node-sass in scripts, does it need to be a local install? – Sam Woolerton Feb 28 '17 at 23:17
  • 2
    Not sure why it happens, npm-run-script works with packages installed in both globally & locally. ***In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts.*** from [npm-run-scripts](https://docs.npmjs.com/cli/run-script) – Bhavik Mar 01 '17 at 15:09
  • Yarn: `yarn global add node-sass` – Oliver Ni Sep 04 '17 at 15:25
4

You can simply run this code

  1. npm install -g sass
  2. sass --watch sass:css

Hopefully work

1

npm commands check "node_package" folder and try to run things there. You can try

npx run scss 

to install scss and then run it, even if it is not installed before.

Onat Korucu
  • 992
  • 11
  • 13
0

The below solves the problem

yarn global add node-sass-chokidar
gaiazov
  • 1,908
  • 14
  • 26
0

This is a simple problem don't worry too much. Just go to package.json file and add this code

"devDependencies": {
    "node-sass": "4.9.2"
},
"scripts" : {
    "node-sass": "node-sass --output-style compressed -o dist/css/ scss --recursive"
}

and just save the file.

And run this command,

npm run node-sass

That's all

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Aathil Ahamed
  • 460
  • 4
  • 16
0

node-sass v4.13+

  1. Install node-sass in your project locally
cd <root path of your project>
yarn add -D node-sass
// or
npm install -D node-sass
  1. Add a script to your package.json
"scripts" : {
...
  "compile:sass": "node-sass --recursive --watch <sass directory> --output <css directory>",
...
}
  1. Run the script from the command line
yarn compile:sass
// or
npm run compile:sass
Isaac Pak
  • 4,467
  • 3
  • 42
  • 48
0

First, run npm install -g node-sass as others have pointed out.

Now, the target of the command (sass.cmd) is not located in the current working directory. For it to still be able to run, its location must be in your PATH (or Path) environment variable.

For me, the path is: C:\Users\Guy\AppData\Roaming\npm

Make sure to restart any terminal/IDE you were trying to run it in before trying again. Otherwise it won't recognize the new environment variable.

Guy Passy
  • 694
  • 1
  • 9
  • 32