It's not very well documented, but you can use npm as a Node.js module and call commands in the code.
I want to capture user input for what packages are required and installing them this way and also save them to the package with the --save-dev flag. I've tried to no avail to get this up and running in code, with it installing but can't find a way to get it to save to the package file.
Is this even possible, or would it have be done another way. Alternate methods are welcome and appreciated.
var npm = require("npm")
npm.load({}, function (er) {
if (er) return handlError(er)
npm.commands.install(["titlecase"], function (err, data) {
if (err) return console.error(err)
})
})
It is possible, flags need to be passed to npm.load():
var npm = require('npm');
npm.load({ 'save-dev': true }, function (err) {
if (err) console.log(err);
npm.commands.install(['lodash'], function (err, data) {
if (err) return console.error(err)
});
});
You have the list of flags and their type here.
Related
**Hi, I don't know what happen here, it's something release with a dependence, It's a code that I didn't write, it's a bundle and I use expo to run my code but, can't open my app because that error please help!! it's for a college project **
function resolveDependencies(parentPath, dependencies, options) {
const resolve = (parentPath, result) => {
const relativePath = result.name;
try {
return [
relativePath,
{
absolutePath: options.resolve(parentPath, relativePath),
data: result
}
];
} catch (error) {
Ignore unavailable optional dependencies. They are guarded with a try-catch block and will be handled during runtime.
if (result.data.isOptional !== true) {
throw error;
}
}
return undefined;
};
const resolved = dependencies.reduce((list, result) => {
const resolvedPath = resolve(parentPath, result);
if (resolvedPath) {
list.push(resolvedPath);
}
return list;
}, []);
return new Map(resolved);
}
Re-traverse the dependency graph in DFS order to reorder the modules and
guarantee the same order between runs. This method mutates the passed graph.
I had the same issue on the latest expo-cli 4.8.1.
For me helped following steps
downgrade from 4.8.1 -> 4.7.3 npm install -g expo-cli#~4.7.3
clear npm cache by executing npm cache clean --force
clear local user cache by deleting everything in C:\Users<user>\AppData\Local\Temp folder.
After these steps, it is working again
I had this issue when running expo start --dev-client on expo-cli version 4.12.1.
I solved it by adding the --clear flag (which clears the Metro bundler cache)
Deleting the system cache Temp folder content will work,
When I face the same issue this solution help me to solve the problem
I am trying to use nodejs with R, how to Execute R algorithm via Node.Js and get the results.
Check out exec, a node module to run terminal statements.
Example:
var exec = require('exec')
exec('code to run my R script', function(err, response) {
if (err instanceof Error) throw err;
if (err) {
console.error('Something went wrong', err);
process.stderr.write(err);
}
// R output
console.log('All good', response);
process.stdout.write(response);
process.exit(1);
});
child_process is also a valid alternative. Checkout this: Run R script and display graph using node.js
Install OpenCPU && RStudio Server
Install node-opencpu client
Begin to use OpenCPU API
I have a Meteor app that needs to call a python script to do some stuff in the background. How can I get this to work? I've tried using child_process and exec, but I can't seem to get it to execute properly. Where should the script even be located?
Thanks
I have same problems and its solved use python-sheel an npm packages to run Python scripts from Node.js
Install on meteor :
meteor npm install --save python-shell
There is simple usage :
var PythonShell = require('python-shell');
PythonShell.run('my_script.py', function (err) {
if (err) throw err;
console.log('finished');
});
If you want run with arguments and options :
var PythonShell = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'path/to/python',
pythonOptions: ['-u'],
scriptPath: 'path/to/my/scripts',
args: ['value1', 'value2', 'value3']
};
PythonShell.run('my_script.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
here detail about python-shell https://github.com/extrabacon/python-shell
Thanks extrabacon :)
I'd like to add a self-updating feature to a globally installed module. Is there a better way of doing it than this?
require("child_process").exec("npm update -g module-name");
There's some documentation about installing npm as a local dependency. Is this necessary? Is there any sample code on how to execute commands like update or install ?
Here's what I've usually done to use the system copy of npm instead of installing another copy of npm as a local module:
function loadNpm(cb) {
require('child_process').exec('npm', function(err, stdout, stderr) {
var m = /npm#[^ ]+ (.+)\n/i.exec(stdout);
if (!m)
return cb(new Error('Unable to find path in npm help message'));
cb(undefined, require(m[1]));
});
}
// usage ...
// only need to call `loadNpm()` once
loadNpm(function(err, npm) {
if (err) throw err;
// load() is required before using npm API
npm.load(function(err, npm) {
if (err) throw err;
// e.g. npm.search('ssh', true, function(err, results) { console.dir(results); });
});
});
Depending on your goals, here are a few options:
1) Via exec() as you mention. Don't forget to add an error callback.
2) Using the npm package as you mention.
For example, I wrote a quick script to install the Yeoman package globally which worked well. I didn't see a lot of documentation for this so I started reading the source code in the npm package itself.
var npm = require('npm');
npm.load (function (err, npm) {
if (err) {
console.log("Error loading");
return;
}
npm.config.set('global', true);
npm.commands.install(['yo'], function (err) {
if (err) {
console.error("Installation failed");
}
});
});
3) Another option is to just have a cron job auto-update packages if that is your goal.
4) You may also be interested in this package https://github.com/tjunnone/npm-check-updates
Is there a way to execute a shell command when creating a grunt-init scaffolding template? For example I would like to execute "bower install" and "git init" after the project is created without having to enter the commands afterwards. The API does not seem to include this functionality.
The template.js is executed by node, so you can use anything node has to offer you.
I've managed to that with the child_process.exec:
var exec = require("child_process").exec;
...
exec("bower install", function(error, stdout, stderr) {
if (error !== null) {
console.log("Error: " + error);
}
done();
});
The only "problem" that I see is that you don't have any logs from bower, so if you are installing many components, it may take a while before any other visual feedback.