Sublime Text 3: Build System - node.js. NPM module not executing

27,555

Solution 1

Sublime text docs:

https://www.sublimetext.com/docs/build_systems.html

shell

Optional. If true, cmd will be run through the shell (cmd.exe, bash…)

Try to add "shell : true

{
  "cmd": ["node-dev", "$file"],
  "selector": "source.js",
  "windows" : {
     "shell": true
  }
}

Solution 2

Following worked for me in Sublime Text 3 on Windows

  1. Tools -> Build System -> New Build System...
  2. Enter the below text in the new file
  3. Save the file as "nodejs.sublime-build"
{
  "shell_cmd": "node ${file}",
  "selector" : "source.js"
}

Prerequisite is to have node.js installed

Solution 3

The command is incorrect for Sublime Text 3 :)

This is one example of running node as build system:

{
    "shell_cmd": "taskkill /F /IM node.exe & node ${file}"
}

Please note that the array-version doesn't work like in Sublime Text 2.

Solution 4

For macOS, this worked for me on Sublime Text 3:

{
    "cmd": ["node","$file","$file_base_name"],
    "working_dir": "${project_path:${folder}}",
    "selector":"source.js"
}

Selector Note

My selector setting was:

"selector":"*.js"

and OdatNurd advised that:

The reason is that the selector is not correct; it doesn't match file names, it matches syntax scopes (i.e. it's based on the syntax in use regardless of file extension); changing it to source.js from *.js should get it working.

Solution 5

If you are a windows user.

Try applying the following snippet

{
    "selector": "source.js",
    "cmd": ["C:\\Program Files\\nodejs\\node", "<", "$file"],
    "windows": {
        "shell": true
    }
}

Save this as node.sublime-build file.

For more info you can refer to http://docs.sublimetext.info/en/latest/ for more.

Share:
27,555

Related videos on Youtube

todd.pund
Author by

todd.pund

Financial Research Developer (II) at Maritz Financial. One wife, two step children, one grandmother, 2 dogs, and a cat. Sometimes I sleep.

Updated on August 18, 2020

Comments

  • todd.pund
    todd.pund over 3 years

    I'm trying to execute node-dev in a sublime text 3 build system. node-dev is in my path:

    cmd node-dev diplay

    Yet when I run this build script:

    {
      "cmd": ["node-dev", "$file"],
      "selector": "*.js"
    }
    

    I get this error, which also shows that npm is in my path.

    npm in path img

    yet when I run with the same build script using node instead of node-dev it executes just fine.

    I've also tried to include the "path" variable pointing at the node-dev bin folder, which didn't help at all.

    Any help would be appreciated. Thanks.

    • vmx
      vmx over 10 years
      Can you try updating full path of node-dev in cmd tag? Eg: ["c:\nodejs\bin\node-dev", "$file"]
  • Tarik
    Tarik over 9 years
    I think this should have been marked as answer. It solved my problem. Thanks!
  • VictorKilo
    VictorKilo almost 9 years
    I think you should add some [escaped] quotes around ${file}. If there are any spaces in the file path the command is broken. "shell_cmd": "taskkill /F /IM node.exe & node \"${file}\""
  • Green
    Green over 8 years
    Where is that .sublime-build file on Windows? It is not in \AppData\Roaming\Sublime Text 3\Packages
  • Peteris
    Peteris about 8 years
    I had to use the following { "shell_cmd": "node ${file/ /\\\\ /}", "selector" : "source.js" } because Sublime Text would otherwise not parse empty spaces in the filename.
  • Anwar
    Anwar almost 8 years
    Works in Linux, too
  • daronwolff
    daronwolff over 7 years
    I used "cmd": ["node", "$file"], instead of node-env. Thanks
  • DrBug
    DrBug almost 6 years
    Actually this is the only way to make it work in Windows 10 and ST3
  • OdatNurd
    OdatNurd over 4 years
    Using a bare $file is not recommended because as @Peteris noted, it doesn't handle spaces in file names properly. That's not a Sublime thing though; the shell_command is passed to the system to interpret so it's the system command processor that has a problem. Better to use node \"$file\" instead so that the filename is quoted for the shell (the regex also works but arguably quoting is a bit easier to interpret).