【问题标题】:can't open javascript files with phantomjs via bash无法通过 bash 打开带有 phantomjs 的 javascript 文件
【发布时间】:2015-11-27 04:52:50
【问题描述】:

我刚刚开始在 osx 10.10.4 上通过终端/bash 版本 2.5.3 (343.7) 试验 phantomjs。即用型二进制文件已安装。

我能够通过符号链接在我的 PATH 上设置 phantomjs。在此之前,我必须直接启动可执行文件或输入其绝对路径。

当我尝试使用 phantomjs 启动 .js 文件时,例如 hello.js,我得到“无法打开 'hello.js'”。如果我输入 .js 文件的绝对路径,它将起作用:

console.log('Hello, world! It works :D');
phantom.exit();



Johns-MacBook-Pro:~ john$ phantomjs hello.js
Can't open 'hello.js'

VS

console.log('Hello, world! It works :D');
phantom.exit();


Johns-MacBook-Pro:~ john$ phantomjs /Users/john/Documents/phantomjs-1.9.2-macosx/examples/hello.js 
Hello, world! It works :D
Johns-MacBook-Pro:~ john$ 

我的问题是:我是否需要符号链接 .js 文件所在的文件夹 位于 /usr/local/bin/ 以确保它也在我的 PATH 上,或者是否有其他方式 .js文件可以用phantomjs启动,不用每次都输入绝对路径?

我在发布这个问题之前进行了搜索,但我不确定我应该如何解释我发现的所有内容 :( 我发现引用 node.js 作为默认执行 .js 文件的最重要且有些相关的信息终端。

请注意,目前我使用的 hello.js 文件位于

/Users/john/Documents/phantomjs-1.9.2-macosx/examples/hello.js

而phantomjs位于

/Users/john/Documents/phantomjs-1.9.2-macosx/bin/phantomjs

如果您需要更多信息,请告诉我,感谢您抽出宝贵时间阅读我的问题! :)

【问题讨论】:

    标签: javascript node.js bash phantomjs casperjs


    【解决方案1】:

    您需要在文件所在的文件夹中才能直接运行它。如果您位于 /Users/john/Documents/phantomjs-1.9.2-macosx/examples/ 文件夹中,您应该能够运行 phantomjs hello.js。

    # Change directory to where the file is you want to run in phantom
    cd /Users/john/Documents/phantomjs-1.9.2-macosx/examples/
    
    # Run the file with phantom
    phamtomjs hello.js
    

    【讨论】:

    • @dryor 啊!现在可以了! :) 非常感谢您简洁明了的回答!这是从 bash 运行文件(PATH 上的可执行文件除外)的一般规则吗?
    • @dryor 不错!非常感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多