2016-11-28 129 views
0

我使用流星1.4.2.3,我想導入NodeJs網絡模塊?我試圖在服務器端用import net from 'net';導入它,但那不起作用。 Net未定義。導入nodejs模塊

import net from 'net'; 

export class Print { 
    private printAsync(callback){ 
    let client = new net.Socket(); 

    } 
} 

回答

0

從一個新鮮的meteor create項目,以下適用於我。

/server/main.js

import { Meteor } from 'meteor/meteor'; 

Meteor.startup(() => { 
    // code to run on server at startup 
}); 

import net from 'net'; 

export class Print { 
    printAsync(callback){ 
    let client = new net.Socket(); 
    console.log(client); 
    } 
} 

console.log(net); 
const print = new Print(); 
print.printAsync(); 

輸出在控制檯:

I20161129-07:59:18.007(-8)? { createServer: [Function], 
I20161129-07:59:18.008(-8)? createConnection: [Function], 
I20161129-07:59:18.008(-8)? connect: [Function], 
I20161129-07:59:18.008(-8)? _normalizeConnectArgs: [Function: normalizeConnectArgs], 
I20161129-07:59:18.008(-8)? Socket: { [Function: Socket] super_: { [Function: Duplex] super_: [Object] } }, 
I20161129-07:59:18.008(-8)? Stream: { [Function: Socket] super_: { [Function: Duplex] super_: [Object] } }, 
I20161129-07:59:18.009(-8)? Server: 
I20161129-07:59:18.009(-8)? { [Function: Server] 
[...] 
I20161129-07:59:18.012(-8)? Socket { 
I20161129-07:59:18.013(-8)? _connecting: false, 
I20161129-07:59:18.013(-8)? _hadError: false, 
I20161129-07:59:18.013(-8)? _handle: null, 
I20161129-07:59:18.013(-8)? _parent: null, 
I20161129-07:59:18.013(-8)? _host: null, 
I20161129-07:59:18.013(-8)? _readableState: 
I20161129-07:59:18.020(-8)? ReadableState { 
I20161129-07:59:18.020(-8)?  objectMode: false, 
I20161129-07:59:18.020(-8)?  highWaterMark: 16384, 
[...] 

我無法重現您不確定net

+0

我正在導入它在服務器端,但導入不起作用。 –

+0

呵呵,它適合我。你有樣品回購還是你能提供更多的上下文? – kooc

+0

我更新了問題。變量網絡是未定義的。你是如何導入網絡模塊的?你安裝了npm包嗎? –