I installed the following package over the command prompt:
npm install -g ar-drone
and then hit via Node.js
var arDrone = require('ar-drone');
and it gives me cannot find module 'ar-drone'
I thought when I install something with -g it will be installed globally?
What did I do wrong?
Check whether the module is downloaded in /usr/local/lib/node_modules if you use ubuntu. Thats the global location for node_modules.
Also you can try to install in your local directory.
Yes I had to transfer all code from index.js to repl.js. My repl.js looks like so and works.
let arDrone = exports;
exports.Client = require('./lib/Client');
exports.UdpControl = require('./lib/control/UdpControl');
exports.PngStream = require('./lib/video/PngStream');
exports.UdpNavdataStream = require('./lib/navdata/UdpNavdataStream');
exports.createClient = function(options) {
let client = new arDrone.Client(options);
client.resume();
return client;
};
exports.createUdpControl = function(options) {
return new arDrone.UdpControl(options);
};
exports.createPngStream = function(options) {
let stream = new arDrone.PngStream(options);
stream.resume();
return stream;
};
exports.createUdpNavdataStream = function(options) {
let stream = new arDrone.UdpNavdataStream(options);
stream.resume();
return stream;
};
let client = arDrone.createClient();
client.createRepl();
Related
From the command line I can start the tsc compiler like this:
../../node_modules/.bin/tsc
I want to incorporate this into a node build script.
There is a typescript compiler for node but it seems much more work to set up rather than just shelling out. You have to pull in all the right files etc.
I have this code :
fs.emptyDirSync(paths.appBuild);
const json = ts.parseConfigFileTextToJson(tsconfig, ts.sys.readFile(tsconfig), true);
const { options } = ts.parseJsonConfigFileContent(json.config, ts.sys, path.dirname(tsconfig));
options.configFilePath = paths.tsConfig;
options.outDir = outDir;
options.src = src;
options.noEmitOnError = true;
options.pretty = true;
options.sourceMap = process.argv.includes('--source-map');
let rootFile = path.join(process.cwd(), 'src/index.tsx');
if (!fs.existsSync(rootFile)) {
rootFile = path.join(process.cwd(), 'src/index.ts');
}
const host = ts.createCompilerHost(options, true);
const prog = ts.createProgram([rootFile], options, host);
const result = prog.emit();
But This will miss files that are not imported in the RootFile.
How can I simply shell out to the tsc exe from node?
You could use child_process.exec:
const path = require('path');
const { exec } = require('child_process');
const tscPath = path.join(__dirname, '../../node_modules/.bin/tsc');
const tsc = exec(`${tscPath} ${process.argv.slice(2).join(' ')}`);
tsc.stdout.on('data', data => console.log(data));
tsc.stderr.on('data', data => console.error(data));
tsc.on('close', code => console.log(`tsc exited with code ${code}`));
I have taken reference from few sites
node js topclient
taobao node package
node-taobao-topclient
What I did:
I installed node-taobao-topclient
My code:
TopClient = require('node-taobao-topclient');
const client = new TopClient({
'appkey': 'xxxx',
'appsecret': 'xxxxx',
'REST_URL': 'http://gw.api.taobao.com/router/rest'
});
client.execute('taobao.wlb.imports.general.consign', {
"session" : "620260160ZZ61473fc31270a2c1f5dcc0efdff78b4c58312482635690",
'trade_order_id':'245033103766976',
'resource_id':'5044440108577',
'store_code':'Tran_Store_775585',
'first_logistics':'123',
'first_waybillno':'123',
'sender_id':'228',
'cancel_id':'228'
}, function(error, response) {
if (!error) console.log(response);
else console.log(error);
})
When I run above code I get error :
TypeError: TopClient is not a constructor
As I am new to node I don't how to exactly use such packages as on reference websites they are using it like:
TopClient = require('./topClient').TopClient;
My node-taobao-topclient package looks like below:
Any guidance on how I can use this API in node would be highly appreciated.
I have used for instalation :
npm i taobao-topclient
I have a correct compilation with :
const TopClient = require('taobao-topclient');
const client = new TopClient({
'appkey': aliexpress_app_key,
'appsecret': aliexpress_app_secret,
'REST_URL': 'http://gw.api.taobao.com/router/rest'
});
I have read the library and they do an exports directly from TopClient, so is not necesary to call it again , in the library at the top :
const request = require('request')
const util = require('./topUtil')
module.exports = class TopClient {
constructor (options) {
const opts = options || {}
if (!opts.appkey || !opts.appsecret) {
...............
If you call it again with another point .TopClient, dont found anything.
The following fragament works well:
TopClient = require('node-taobao-topclient').default;
I have a module that I use for node and (now) react-native. I'd like to selectively export code depending on which platform it's going to run on. If you can get this to run on the device, you've solved the problem.
module:
if(!react_native){
exports.fs = require('fs');
}
exports.print = function(str){ console.log(str); }
on device:
var m = require('module');
m.print("hello world.");
Is there any way to do this?
I don't want to create two separate modules that only differ in index.js if I don't have to.
Thanks!
A simple check - loading the basic package:
var isNative = false;
var Platform;
try {
Platform = require('react-native').Platform;
isNative = true;
} catch(e) {}
if (isNative) {
console.log(Platform.OS, Platform.Version);
} else {
console.log('node');
}
I want to import a dependency of a dependency. For example, I want to import jade-load directly into my app:
my-app
┗━jade
┗━jade-load
I could do require('jade/node_modules/jade-load'), but this won't work if the node_modules tree has been flattened or deduped.
I thought of using require.resolve() to find out where jade-load really is, but there doesn't seem to be a way to tell it the starting point for the resolution. I need to be able to say "require jade-load from wherever jade is".
NB. I do not want to install jade-load as a direct dependency of my app; the point is to import the same instance that jade uses, so I can monkeypatch it.
I guess you may want to use proxyquire for managing dependencies of required modules. You can set proxyquire to globally override methods of the submodule when it will be loaded.
main.js
var proxyquire = require('proxyquire');
// use default childModule properties and methods unless they are redefined here
var childModuleStub = {
/* redefine here some necessary methods */
'#global': true
};
// parent module itself does't require childModule
var parentModuleStubs = {
'./childModule': childModuleStub
};
var parentModule = proxyquire('./parentModule', parentModuleStubs);
var result;
result = parentModule.exec();
console.log(result);
childModuleStub.data.sentence = "Overridden property.";
result = parentModule.exec();
console.log(result);
childModuleStub._exec = function () {
return "Overridden function.";
};
result = parentModule.exec();
console.log(result);
parentModule.js
var intermediateLibrary = require('./intermediateLibrary');
module.exports = {
exec: function() {
return intermediateLibrary.exec();
}
};
intermediateLibrary.js
var childModule = require('./childModule');
module.exports = {
exec: function() {
return childModule._exec();
}
};
childModule.js
var lib = {};
lib.data = {};
lib.data.sentence = "Hello, World!";
lib._exec = function () {
return lib.data.sentence;
};
module.exports = lib;
Results:
Hello, World!
Overridden property.
Overridden function.
I am using nodejs in my meteor app and I added packages using mrt add npm and then in my client directory in packages.json I added skimlinksjs and its version and it is added to my app.
When I tried to using them in my app in server side code like this,
var res;
var skim = Meteor.require('skimlinksjs');
var apili = Meteor.require('/config.js');
skim.setup(apili.key);
skim.query({
searchFor: "title:\"moto g\"",
fq: "country:US"
}, function(err,data) {
res=data.skimlinksProductAPI.numFound;
}
);
return res;
and my config.js file is like this
module.exports = {
key: "xxxxxxx"
}
whenI'm running this application it is showing error like
module not defined
What went wrong with my code or is there any need to install other packages?
I just got the answer
Write this function in server side code
function returnAllResult()
{
var skimlinks = Meteor.require('skimlinksjs');
skimlinks.setup("xxx");
var skimlinks_query = Async.wrap(skimlinks.query);
var result = skimlinks_query({
searchFor: "title:\"moto g\"",
fq: "country:US",
rows:5
});
return result;
}
to know about asynchronous functions watch this
and then in my server side methods call this
apiresult:function()
{
var response = returnAllResult();
return response.skimlinksProductAPI.products[0].merchant;
}
that's it working fine now. Hope this helps someone