无论出于何种原因,在Node.js中,函数process.send在某些环境中定义,但在其他环境中未定义.例如,当我从Node.js中的父进程分叉子进程时,如下所示:
//parent process
var cp = require('child_process');
var k = cp.fork('./child.js',['arg1','arg2','arg3']);
k.send('fail'); //k.send is defined...
process.send("ok, let's try this..."); //process.send is NOT defined
Run Code Online (Sandbox Code Playgroud)
在子进程内:
//child.js
process.send('message'); //process.send is defined, and will send a message to the parent process above
Run Code Online (Sandbox Code Playgroud)
为什么process.send在某些Node.js进程中有条件地定义而不是其他进程?看起来像Node.js架构师的糟糕设计决策.
我知道如何解决这个问题的唯一方法是:
if(typeof process.send === 'function'){
process.send('what I want to send')
});
Run Code Online (Sandbox Code Playgroud) 使用node.js Cluster模块,可以直接获取工作进程的id.
https://nodejs.org/api/cluster.html
那将是:
cluster.on('fork', function (worker) {
console.log('A worker', worker.id, 'was forked.');
});
Run Code Online (Sandbox Code Playgroud)
但是我如何id从工人本身获得工人的工作?为什么当集群分叉工作时,集群模块不会为worker提供id?
我是否真的必须从主进程发送它的集群ID?
我正在寻找类似于:
process.id (与process.pid相对)
要么
process.worker.id
Run Code Online (Sandbox Code Playgroud)
无论如何,我无法弄清楚工人身份的内容是什么.
我在Angular5项目中有这个import语句:
import {plugins, SCECodeGenType} from 'sce-plugins/code-generation';
Run Code Online (Sandbox Code Playgroud)
这解析了我的文件系统上的这个路径:
/Users/.../suman-chrome-extension/node_modules/sce-plugins/code-generation/index.d.ts
Run Code Online (Sandbox Code Playgroud)
在构建应用程序时ng build -w,我收到此错误:
ERROR in ../sce-plugins/code-generation/index.ts Module build failed: Error: /Users/alexamil/WebstormProjects/oresoftware/sumanjs/sce-plugins/code-generation/index.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
at AngularCompilerPlugin.getCompiledFile (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman-chrome-extension/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:662:23)
at plugin.done.then (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman-chrome-extension/node_modules/@ngtools/webpack/src/loader.js:467:39)
at <anonymous> @ ./src/app/shared/services/lang.service.ts 14:24-62 @ ./src/app/app.module.ts @ ./src/main.ts @ multi ./src/main.ts
Run Code Online (Sandbox Code Playgroud)
我相信这是因为我npm link用来链接'sce-plugins'项目以进行本地开发.
我npm link在这里看到与Angular5项目一起使用的一些问题:
https://github.com/angular/angular-cli/issues/3875
https://github.com/angular/angular-cli/issues/8677
https://github.com/angular/angular-cli/issues/9376
有谁知道如何解决?
更新:
它似乎与npm linkperse或符号链接没有关系.如果我只是将本地目录复制到node_modules/sce-plugins,我会得到同样的错误. …
我的编辑(Webstorm)给了我这个奇怪的信息:
二进制操作参数类型字符串与类型字符串不兼容
以下是导致警告消息的代码:
const {masterid = '', type = ''} = req.query;
if (!masterid) {
return res.status(500).send(new Error('Missing query param "masterid".'));
}
async.autoInject({
patients(callback) {
if(type && type !== 'staff'){
return process.nextTick(callback);
}
store.get(`chat:presence:users:${masterid}`, callback)
},
users(callback) {
if(type && type !== 'patient'){
return process.nextTick(callback);
}
store.get(`chat:presence:patient:${masterid}`, callback);
}
}, (err, results) => {
if (err) {
return res.status(err.code).send(err);
}
res.json(results);
});
Run Code Online (Sandbox Code Playgroud)
也许我使用错误的语法?
我只是想设置变量的默认值..语法应该是正确的.
如果您有这样的RequireJS模块:
define(
[
'#patches',
'backbone',
'underscore',
'react',
'#allCollections',
'#allModels',
'app/js/routers/router',
'#allTemplates',
'#allControllers',
'#allRelViews'
],
function(){
var patches = arguments[0];
});
Run Code Online (Sandbox Code Playgroud)
有没有办法知道哪个依赖首先被加载?就我而言,'#patches'是一些window.X实用程序函数,我想在其他任何东西之前加载.我是否需要以不同的方式配置以确保这一点?
(在我的例子中,"#"只是我自己的表示法,表示一个模块,其路径是在我的主配置文件中预定义的)
如果我们在Node中派生一个child_process,我们如何将节点参数传递给child_process?
https://nodejs.org/api/child_process.html
具体来说,我想产生~20个进程,并希望通过使用--v8-options限制每个进程的内存使用量,但我找不到任何这样做的例子 - 这是可能的还是子进程假设与父节点相同的节点参数?
父母将是:
node foo.js
Run Code Online (Sandbox Code Playgroud)
孩子们会
node --some-flag=bar baz.js
Run Code Online (Sandbox Code Playgroud)
...
我希望使用传递节点选项
child_process.fork()
Run Code Online (Sandbox Code Playgroud)
但如果它只有可能
spawn()
Run Code Online (Sandbox Code Playgroud)
要么
exec()
Run Code Online (Sandbox Code Playgroud)
然后我想我会拿走我能得到的东西.
举个简单的例子,下面的代码不会运行带有--harmony标志的Node.js
var cp = require('child_process');
var args = ['--harmony'];
var n = cp.fork(filePath, args , Object.create(process.env));
Run Code Online (Sandbox Code Playgroud) 我有一个NPM项目,npm install运行时,我想运行一个自定义脚本.
我尝试在package.json中使用它:
"scripts": {
"ng": "ng",
"start": "ng serve",
"install": "./scripts/install.sh", // <<<<
},
Run Code Online (Sandbox Code Playgroud)
但这实际上只是导致无限循环.我正在寻找这个的原因是因为有些工具可以简单地调用npm install,所以我无法控制它们运行的内容.否则,如果我有控制权,我会打电话给./scripts/install.sh自己.
请注意,这可能不是最好的主意,只是好奇,如果可能的话.
请注意我的安装脚本如下所示:
#!/usr/bin/env bash
export FOO="bar";
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true";
npm install
Run Code Online (Sandbox Code Playgroud) 我在 Github 上有一个 git 仓库,看起来像这样:
.git/
assets/
tools/
a.sh
b.sh
c.sh
Run Code Online (Sandbox Code Playgroud)
当然 .git 文件夹不在版本控制中,但您明白了。我想将 tools/*.sh 复制到本地目录,路径如下:
./scripts/git/tools
Run Code Online (Sandbox Code Playgroud)
我当前的目录是一个 git 存储库,它与远程 git 存储库共享完全不同的历史记录,我只想从远程复制一些文件,我不想合并任何内容。
我想我可以做这样的事情:
git clone --path assets/tools/*.sh "$remote_repo" ./scripts/git/tools
Run Code Online (Sandbox Code Playgroud)
但该命令不是真实的,我可以使用一些命令来执行此操作吗?
我能想到的唯一其他方法是将整个存储库克隆到某个临时目录,然后复制文件,但这并不是那么有趣。
node.js ×6
javascript ×3
angular ×1
angular-cli ×1
angular5 ×1
fork ×1
git ×1
git-clone ×1
git-subtree ×1
github ×1
npm ×1
requirejs ×1
tsc ×1
typescript ×1
webstorm ×1