简体   繁体   中英

Unable to install and run my own npm module in Linux

I've created a npm module that I intend to publish, but not without testing that it works first. So I install the module I'm working with, npm install -g . and it works well on my Windows computer, but it won't run on my Linux (Debian) computer. Instead I get the following error:

15:52 $ transval
: No such file or directory

The only thing I've found so far when I compare the generated cmd and bash file on my windows computer is that whilest (when comparing to, say, 'gulp') the cmd-files are identical in structre the bash files are not. The second line, where the basedir is set differs. This the full output of the published bash file for my module:

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/node_modules/transval/bin/transval.bin.js" "$@"
  ret=$?
else 
  node  "$basedir/node_modules/transval/bin/transval.bin.js" "$@"
  ret=$?
fi
exit $ret

But if I compare the top two lines with any other file there is a significant difference! This is the top two lines from any other module, such as gulp:

#!/bin/sh
basedir=`dirname "$0"`

All other bash files get that dirname. If I change my bash file to that basedir it all of a sudden works. It is driving me mad!

EDIT: These two files are created when I run the command npm install -g . (thus installing my package globally for testing) or when I have published (ie npm publish ), so I'm not generating these files my self.

My package.json has a bin entry which points at a file that looks like this:

#!/usr/bin/env node

var app = require('../bundle.js');
app.init(process.argv);

Anyone have any idea why it would work on Windows and not in Linux?

Edit Per additional information in the OP's answer , it is indeed a line-ending problem. The problem actually is not related to $() vs. `` .

When generating on Windows, the lines end with a carriage return and a linefeed, \\r\\n . However, when running the generated script on Debian, only the \\n is taken as the end of line. As a result, the assignment to basedir is effectively:

basedir=$(dirname "...")$'\r'
                      # ^^^^^ Carriage return!  Oops!

I think that is why the error message was ': No such file or directory': before the : , the contents of $basedir were actually printed, ending with the \\r . The \\r moved the cursor back to the beginning of the line, then the rest of the error message, beginning with : , overprinted the path. (That's a guess, though — I can't reproduce the exact error message on my system.)

A workaround is to put a# (space-hash) after the basedir assignment:

basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") #
                                         # add these ^^

That way the \\r will be part of a comment and not part of basedir .


Note: see this question and its answers for other ways of getting $basedir .


Per chat, the OP is going to add the $basedir values for both options tomorrow.

For reference, here's where we are at present:

  • Per this answer , npm generates the wrapper scripts based on bin entries in the package.json .
  • npm uses the cmd-shim module to make the scripts.
  • cmd-shim was updated 2013/10/28 to use the dirname ... echo ... sed sequence so that it would work on msysgit.
  • gulp and other scripts using dirname "$0" were presumably generated with a cmd-shim predating that update.
  • The OP's Debian /bin/sh is apparently dash (currently 0.5.7-4 in debian stable ).
    • for the OP, on Debian, bash is version: 4.3.46(1)-release and sed is version 4.2.2
    • I tried both basedir types on my Cygwin dash 0.5.8-3 and they both worked.

On Ubuntu, the OP has a different problem: /usr/bin/env: 'node\\r': No such file or directory . That looks like a line-ending issue to me, probably different from the Debian issue.

Ok, found the problem. It seems to have been a problem with publishing from Windows. Once I had published from Linux (Ubuntu in this case) I could install it on both Linux and Windows computers. I'm not sure what the reason for this is, be it some npm bug or an issue with doze line breaks, but now it's working :)

I did try to publish previously from Linux, and failed, but with an old version of Node (4.something) and that didn't work but now I've upgraded to the latest version and it works well, so that might've had something to do with it.

Edit: I can now verify that publishing on a Debian machine running node 6.2.2 creates an unusable published version whereas publishing on a Ubuntu machine running node 7.4.0 works well and can be installed and run anywhere. Both machines are running npm version 4.0.5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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