nodejs cannot find module 'zombie' with PHP mink

后端 未结 1 1483
孤街浪徒
孤街浪徒 2020-12-07 04:24

I\'m trying out Mink (PHP) on Ubuntu 14.04; I basically did the following:

$ apt-show-versions nodejs
nodejs:amd64/trusty 0.10.45-1nodesource1~trusty1 uptoda         


        
相关标签:
1条回答
  • 2020-12-07 04:54

    Ok, found some sort of a method which seemingly works - but I'd still like someone more knowledgeable to answer.

    Anyways, the trick is - zombie can accept a path to the nodejs binary; so if you cannot really pass environment variables for nodejs from PHP, then make a shell script which will set these environment variables, and then call nodejs.

    First this was my install:

    # remove previous
    sudo npm uninstall -g zombie --save-dev
    sudo apt-get remove --purge nodejs && sudo apt-get autoremove --purge
    
    # install new
    curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
    nvm install iojs-v3.3.1
    npm list -g --depth=0
    nvm install 4.0.0
    npm list -g --depth=0
    npm -g install zombie --save-dev
    

    The problem with nvm is that it installs in a user directory, and I'd like to test my scripts both on my user machine and remote server, where my uids are completely different. Regardless, using a custom executable helps a bit with that. So, create a script in a "global" location, I chose /home, so I'll need sudo to create files there:

    sudo touch /home/node_pth.sh
    

    ... then paste in the following content:

    #!/bin/bash
    export NODE_PATH=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules
    #echo ARGS ARE "$@" | tee -a /tmp/node.log
    /home/USERNAME/.nvm/versions/node/v4.0.0/bin/node "$@"
    

    ... of course, replacing the paths with your correct ones; then finally make it executable:

    sudo chmod +x /home/node_pth.sh
    

    Now we can use the following test_php_mink.php PHP file:

    <?php
    
    $nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; # correct NODE_PATH, but will not help
    $nodePath = "/home/node_pth.sh"; # shell script that sets NODE_PATH, then calls node executable
    
    echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; #
    putenv("NODE_PATH=".$nodeModPath);
    echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; # is there - but still doesn't help with call
    
    # composer autoload:
    require_once __DIR__ . '/vendor/autoload.php';
    
    echo "safe_mode: '" . ini_get("safe_mode") ."'\n"; # have PHP 5.5.9, safe_mode is removed
    
    
    $driver = new \Behat\Mink\Driver\ZombieDriver(
      //~ new \Behat\Mink\Driver\NodeJS\Server\ZombieServer()
      # copy defaults here for everything but nodeBin;
      # see vendor/behat/mink-zombie-driver/src/NodeJS/Server.php
      new \Behat\Mink\Driver\NodeJS\Server\ZombieServer("127.0.0.1", 8124, $nodePath, null)
    );
    
    $session = new \Behat\Mink\Session($driver);
    
    // start the session
    $session->start();
    ?>
    

    ... OR, I just realized there is setNodeModulesPath($nodeModulesPath) in vendor/behat/mink-zombie-driver/src/NodeJS/Server.php, so we can drop the proxy bash executable altogether:

    <?php
    
    $nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; # correct NODE_PATH, but will not help via putenv
    
    echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; #
    putenv("NODE_PATH=".$nodeModPath);
    echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; # is there - but still doesn't help with call
    
    # composer autoload:
    require_once __DIR__ . '/vendor/autoload.php';
    
    echo "safe_mode: '" . ini_get("safe_mode") ."'\n"; # have PHP 5.5.9, safe_mode is removed
    
    $zsrv = new \Behat\Mink\Driver\NodeJS\Server\ZombieServer();
    $zsrv->setNodeModulesPath($nodeModPath . "/"); # needs to end with a trailing '/'
    
    $driver = new \Behat\Mink\Driver\ZombieDriver( $zsrv );
    
    $session = new \Behat\Mink\Session($driver);
    
    // start the session
    $session->start();
    
    ?>
    

    Anyways, when this script is called, it outputs:

    $ php test_php_mink.php
    NODE_PATH is: ''
    NODE_PATH is: '/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules'
    safe_mode: ''
    

    ... and as there are no errors, I'm assuming it is all fine now...

    0 讨论(0)
提交回复
热议问题