npm package.json bin won't work on Windows - windows

I am trying to start my cli tool via the package.json bin property.
I have the following:
...
"name": "mycli",
"bin": "./bin/mycli",
...
When I open the cmd in the package path and type: "mycli" it says that the command is not recognized.
Should I run an npm command? or use the scripts property? am I trying to access the bin property incorrectly?

Try to specify the name of your cli tool in the bin property, like:
"bin": {
"mycli": "./bin/mycli" // or "/bin/mycli.js" if it's a .js file
}
Then, run npm link, from inside your project folder, to create a global symbolic link to the current folder.
Don't forget to add the "preferGlobal": "true" property just before the bin property in your package.json file, in order to warn users to install your module globally.

Whenever I was trying to get my app to link, I kept running into problems on Windows where the generated scripts that would execute on path would try to run the *.js file using the default Windows executable (I don't know what that would be). I'm not sure why. I think it might be because it is a JavaScript file. However, I compared the generated scripts to some of the other modules I had installed, and figured out that if I made the bin file referenced by the package.json act as though it were to be executed on a *nix machine, npm would automatically try and add the call to node.
For example:
If my package.json looks like this:
myapp/package.json
"name": "myapp",
"bin": {
"myapp": "./bin/myapp"
}
My referenced bin file looks like this:
myapp/bin/myapp
#!/usr/bin/env node
require("../server.js");
The 2 generated executable files that appear in %APPDATA%\npm show up as follows by running the command npm link from within the myapp directory (which would have package.json in the root):
myapp
#!/bin/sh
basedir=`dirname "$0"`
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/node_modules/myapp/bin/myapp" "$#"
ret=$?
else
node "$basedir/node_modules/myapp/bin/myapp" "$#"
ret=$?
fi
exit $ret
myapp.cmd
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\node_modules\myapp\bin\myapp" %*
) ELSE (
node "%~dp0\node_modules\myapp\bin\myapp" %*
)
Bear in mind, I didn't need to make the 2 files above explicitly, I just needed to have the file to be executed as the bin file in the package.json. npm did the file creation.
Line Endings
One other thing to note that I ran into while using this method, make absolutely sure that your line endings are correct. I noticed that my bin was erroring with: ": No such file or directory" whenever I installed on *nix machines because there was an incorrect line ending. Thanks to View line-endings in a text file for example of how to print visible line endings.
For example, if you run cat -e PATH_TO_BIN and get something like this:
#!/usr/bin/env node^M$
^M$
require("../index.js");^M$
You're using the wrong line endings. If you get something like this:
#!/usr/bin/env node$
$
require("../index.js");$
Those should be the right line endings.

If you put
#!/usr/bin/env node
in the first line of your script, npm will create the necessary wrapper scripts.

Answer from Rodrigo Medeiros works for me, but only if I have too the shebang line at the .js file.
There I had another issue. I have node.js installed at c:\Program files\nodejs, and this was my shebang line:
#!c:/program files/nodejs/node
This didn't work, because the blank space. This was the correct one:
#!c:/progra~1/nodejs/node

Related

path in makefile not working

Im running the following makefile
which needs to change dir to specific target and run there npm install
The problem is that I was able to see in the output that it print the directory (project/app) to the right directory but the installation (npm install) run on level up (project), why ?
For example
When I run it I see from cd $(DIR)/app
/Users/i03432/go/src/project/app
Now the second command is
npm install
And I got error that id doesn’t find the package json in the project path which is right... it’s only in the app path. Why the cd is not working ?
it try to find it here
/Users/i03432/go/src/project/package.json
and here is the package.json
/Users/i03432/go/src/project/app/package.json
The makefile is
module:
DIR=$(PWD)
#echo $(DIR)
cd $(DIR)/app
npm install
Every command in a rule is run in a single process (sub-shell). Every change you perform on the environment is hence tied to that particular line. You want to change your snippet to
cd $(PWD)/app && npm install
This command runs in a single subprocess and should yield the desired result. Note that this problem occurs for the definition of DIR, too, so you might want to move this a few lines up:
DIR = $(PWD)
module:
cd $(DIR) && npm install
This way, you are referring to a variable that make provides, and you don't rely upon subprocesses here.

trying to write a script to run a command in a deep directory on entry to ssh

So I'm trying to write a script that will let me run a command to initialize some things. To be more specific, let's say I start in my home directory but to run this command I want I must be in a directory three folders deep into the home directory.
My script looks generically like this.
#!/bin/sh
cd home/path/to/final/directory/
command
Now usually, when I cd to this directory I can run the command on the command line and everything works fine.
When I tried to use a script to do it, the command line throws an error saying that that command isn't recognized like the computer doesn't know where to look.
The temporary fix I used was making a symbolic link to the directory I wanted but I was hoping someone could help me so that when I ssh to this node this script can be run immediately so I will not have to go into the deep directory, run the command and leave again.
Try defining full paths, for example:
#!/bin/sh
cd $HOME/path/to/final/directory && /path/to/your/command
In this case, it will try to cd into your defined directory but if it can't find the dir it will not run the command, this because of the &&
To test before running the command you could do a ls, for example:
cd $HOME/path/to/final/directory && ls

bash: ngc: command not found

I'm using #angular/compiler-cli to build my ng2 app in aot mode. When I input 'ngc -p tsconfig-aot.json' in my bash window, I get 'bash: ngc: command not found'. However, when I use 'node_modules/.bin/ngc -p tsconfig-aot.json' instead, it works. I googled for serval times but didn't get any usfull information. Can any give me a hand? Thx!
Seems like you need to put ngc in your path:
echo $PATH
Do you see ngc in binary in your path?
If not:
PATH=$PATH:/path/to/ngc
To make it permanent add to .bash_profile
export PATH=$PATH:/path/to/ngc
I've tried to change the slash to 'backslash' on windows and it worked for me:
node_modules\\.bin\ngc
If you don't want to set it globally, you can specify an absolut path in your angular-project, just make sure that you delete this part of the path when you don't use it anymore.
ngc is in node_modules/.bin, so depending on where you want to use ngc you can export the path like this:
PATH=$PATH:../../../node_modules/.bin
To run commands located into the node_modules folder of your project, without installing them globally (operation that will make the ngc command work in any system folder), you can use this command:
ngx ncc <options>
Basically ngx is a shortcut that executes any command located in node_modules bin folder.

how to run script with simple command

I have project structure like this
project
|app
|script
inside script folder, there are files such as 'run'
run file content:
#!/bin/bash
npm start
I want to run the file 'run' while I'm at the root of my project by typing only command 'run'. How would you do this?
This is sh file. In order to execute sh file on linux this file has to be executable.
Make sure this file has X permission.
If there is no x permission on file simply execute the command
chmod +x run.sh
Then execute the file by typing
./run.sh
For windows you need to create .bat file.
I'm not quite sure what you want but assuming you need a way to execute a file from node.js, you can use child_process module and child_process.exec method to start any executable.
Assuming the run file in the script directory is executable (if not, run chmod +x script/run), it can be executed by running ./script/run.
If you want to avoid having to type the name of the directory (script), you could append the script directory to your PATH environment variable. If you’re running a POSIX compatible shell (not csh or tcsh), this can be done using:
export PATH="$PATH:/path/to/project/script"
This will allow you to run any executable command in the script directory without having to specify the name of the directory, e.g., run.
NB: be sure that there aren’t common command names in the script directory as these commands can be run from any directory (including outside the project directory) after it has been added to the PATH. That’s also why I suggest adding it to the end of the PATH (so it’s the last directory that’s searched for executable commands).

Build system with bash script for Sublime Text 2

I work with SQL queries in Sublime 2. I have a custom script that takes sql filename as an argument, uploads it to the server, downloads the query result and converts it to valid csv file. It works, and it's very useful.
However, when I tried to set up a build system for Sublime Text 2 using this script, I get errors. Here's my sublime-build file:
{
"cmd" : ["exec_sql.sh", "$filename"],
"selector" : "source.sql",
"path" : "/Users/username/sql"
}
SQL files and exec_sql script are both located in /Users/username/sql/ folder. Here's what I get:
/Users/username/sql/exec_sql.sh: line 9: which: command not found
/Users/username/sql/exec_sql.sh: line 16: basename: command not found
/Users/username/sql/exec_sql.sh: line 19: username#hostname.com:/tmp/: No such file or directory
/Users/username/sql/exec_sql.sh: line 24: ssh: command not found
/Users/username/sql/exec_sql.sh: line 25: username#hostname.com:/tmp/.csv: No such file or directory
[Finished in 0.0s with exit code 127]
It seems that despite being a command-line script, sh file is being interpreted as something else. How do I fix it?
I think that the problem can be solved if your custom script is added to the system path (to the BIN directory).
I am going to use a detailed example that i have used succesfully.
Be sure that your custom script is marked as executable and has got the shebang. A simple script that includes the basename command that fails in your example (i have called it test_basename.sh):
#!/bin/bash
echo $1
basename $1
Make a symlink to include the script in the BIN directory so it can be executed in any folder. In my Ubuntu i do it with this command:
sudo ln -s /Users/username/sql/test_basename.sh /usr/bin/test_basename.sh
Check the script in the console in any directory
cd /tmp
test_basename.sh some-filename
Restart Sublime Text
Create a build system that uses the simple script. I have used $file as an example. Other options are in the documentation.
{
"cmd" : ["test_basename.sh", "$file"]
}
Open a file in Sublime Text and run the build system created.
If you don't want to create a symlink use a variable like $project_path or $file_path
{
"shell_cmd": "./do.sh",
"shell": true,
"working_dir": "$project_path"
}
reference for variables: sublimeText docs
I was able to create a general purpose Bash build system for Sublime Text 2:
Create a new Build System
Use the following code:
{
"cmd": ["/path/to/bash-build.sh", "$folder"],
"shell": true,
"working_dir": "$folder",
"windows": {
"cmd": ["C:\\path\\to\\bash-build.sh", "$folder"]
}
}
Save it as bash.sublime-build
Create a new shell script using your editor of choice and paste the following code into it:
#!/bin/env bash
echo "Start building..."
if [ -f $1/build.sh ]; then
cd $1
build.sh
cd -
else
echo "File doesn't exist: $1/build.sh"
fi
echo "Done."
exit $?
Save it as bash-build.sh in the same folder that you reference in the Sublime Build System
Open a folder in Sublime (note: on Windows, build systems seem broken when Sublime is opened from the command line)
Create a new text file and save it as build.sh into the root folder opened in Subilme
In build.sh, go crazy:
#!/bin/env bash
# do some crazy, custom build
exit $?
I've started using this in conjunction with Pandoc to generate HTML files from Markdown files.
It seems like you are on Mac. I had the same problem before I realized that the System path variable is wrong.
I fixed it the following way.
First, get the current PATH by pasting this in terminal (without first $):
$ echo $PATH
Copy this path (it will look something like this: /opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin)
Now edit the build system to include this path (the path variable you use should be actually a working_dir):
{
"cmd" : ["exec_sql.sh", "$filename"],
"selector" : "source.sql",
"working_dir" : "/Users/username/sql",
"path": "PASTE $PATH VALUE HERE"
}
And it should work now. At least it helped me.

Resources