gpt4 book ai didi

php - Nodejs 无法使用 PHP Mink 找到模块 'zombie'

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:47 26 4
gpt4 key购买 nike

我正在 Ubuntu 14.04 上尝试 Mink (PHP);我基本上做了以下工作:

$ apt-show-versions nodejs
nodejs:amd64/trusty 0.10.45-1nodesource1~trusty1 uptodate
$ npm -v
2.15.1
$ sudo npm install -g zombie
npm WARN engine zombie@4.2.1: wanted: {"node":"^4.0.0"} (current: {"node":"0.10.45","npm":"2.15.1"})
...
zombie@4.2.1 /usr/lib/node_modules/zombie
├── ms@0.7.1
├── debug@2.2.0
...

$ ls /usr/lib/node_modules/zombie/node_modules/
babel-runtime bluebird debug eventsource iconv-lite jsdom lodash mime ms request tough-cookie ws

所以,基本上,即使我收到警告,模块也会构建,并且应该位于目录 /usr/lib/node_modules 中。

然后我这样做:

mkdir test_php_mink
cd test_php_mink/
composer require behat/mink
composer require behat/mink-zombie-driver

作为支票:

test_php_mink$ ls
composer.json composer.lock vendor

...似乎所有 composer 文件都在那里。

最后,根据 http://mink.behat.org/en/latest/drivers/zombie.html (以及 Cannot find module 'zombie' · Issue #84 · assaf/zombie · GitHub ),我正在尝试这个脚本:

<?php

# 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

putenv("NODE_PATH=/usr/lib/node_modules");
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; # OK, is there

# NOPE:
#$driver = new \Behat\Mink\Driver\ZombieDriver();

$driver = new \Behat\Mink\Driver\ZombieDriver(
new \Behat\Mink\Driver\NodeJS\Server\ZombieServer()
);

$session = new \Behat\Mink\Session($driver);

// start the session
$session->start();
?>

不幸的是,这个脚本仍然失败:

$ php test_php_mink.php 
safe_mode: ''
NODE_PATH is: '/usr/lib/node_modules'
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Server process has been terminated: (8) [
module.js:340
throw err;
^
Error: Cannot find module 'zombie'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/path/to/test_php_mink/vendor/behat/mink-zombie-driver/bin/mink-zombie-server.js:3:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
]' in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php:413
Stack trace:
#0 /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php(306): Behat\Mink\Driv in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php on line 413

如何运行这个基本示例?

<小时/>

编辑:对此进行了更多研究,发现当我在命令行上指定环境变量时:

$ NODE_PATH=/usr/lib/node_modules php test_php_mink.php
safe_mode: ''
NODE_PATH is: '/usr/lib/node_modules'
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Server process has been terminated: (8) [
/usr/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:238
var nonInheritedTags = new Set([
^
ReferenceError: Set is not defined
...

...那么模块似乎找到了!所以我的问题基本上简化为:如何从我的 php 脚本更改 NODE_PATH 环境变量,这样我就不必在 shell 上指定它 - 因为显然 putenv("NODE_PATH=/usr/lib/node_modules"); 对我来说并不真正有用...

至于新错误,有 Installing Zombie.js Error: ReferenceError: Set is not defined. What am I doing wrong? - 显然这是由于版本不匹配造成的,我收到了警告 (npm WARN engine zombie @4.2.1: Wanted: {"node":"^4.0 .0"}(当前:{"node":"0.10.45","npm":"2.15.1"})),所以我想我必须安装nvm 这样我就可以安装正确的nodejs版本;我还在 /usr/lib/node_modules/zombie/README.md 中注意到:

Zombie 4.x is tested to work with io.js 1.6 or later.
If you need to use Node 0.12 or earlier, consider using Zombie 2.x. ...
To install Zombie.js you will need io.js:
```bash
$ npm install zombie --save-dev
```

...我认为也可以使用 nvm 安装;所以我会尝试一下...

最佳答案

好吧,找到了某种似乎有效的方法 - 但我仍然希望有更懂行的人来回答。

无论如何,诀窍是 - zombie 可以接受 nodejs 二进制文件的路径;因此,如果您无法真正从 PHP 传递 nodejs 的环境变量,则创建一个 shell 脚本来设置这些环境变量,然后调用 nodejs

首先这是我的安装:

# 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

nvm 的问题是它安装在用户目录中,我想在我的用户计算机和远程服务器上测试我的脚本,其中我的 uid 完全不同。无论如何,使用自定义可执行文件对此有所帮助。因此,在“全局”位置创建一个脚本,我选择了 /home,因此我需要 sudo 在那里创建文件:

sudo touch /home/node_pth.sh

...然后粘贴以下内容:

#!/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 "$@"

...当然,用你正确的路径替换路径;最后使其可执行:

sudo chmod +x /home/node_pth.sh

现在我们可以使用以下test_php_mink.php PHP 文件:

<?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();
?>
<小时/>

...或者,我刚刚意识到 vendor/behat/mink-zombie-driver/src/NodeJS/Server.php 中有 setNodeModulesPath($nodeModulesPath) ,所以我们可以完全删除代理 bash 可执行文件:

<?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();

?>

无论如何,当调用这个脚本时,它会输出:

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

...由于没有错误,我假设现在一切都很好...

关于php - Nodejs 无法使用 PHP Mink 找到模块 'zombie',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37436666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com