I'm writing a Cordova plugin, it has a node dependency for one of the hook scripts. Ideally when my plugin is installed:
$ cordova plugin add my-cordova-plugin
I would like it to run npm install if package.json has dependencies listed.
Does Cordova support this feature in some way? Have I missed something?
My current solution is another hook that runs after_plugin_install:
module.exports = function (context) {
var shell = context.requireCordovaModule('shelljs');
shell.cd(context.opts.plugin.dir);
shell.exec('npm install');
};
I you're looking for to add npm modules to your Cordova project, you don't need a plugin, juste use a simple hook triggered before_prepare.
This hook will run all the npm install you need for each cordova prepare (also for cordova run, cordova compile, etc..).
You don't have to make a JS file for a hook, a linux shell script is enough (although it is less portable). I prefer to use juste .sh file when my only need is to do "npm install" or stuff like that.
Related
Using NPM at the command line, is there some official hook that I can configure, which means that for any npm install command (including npm install x), a certain hook is run?
Right now, I see certain limitations -
if I run npm install x it will not run hooks for another dependency (obviously) but it also doesn't seem to run hooks for the main/parent package.
Plain old npm install will run preinstall / postintall hooks for the main/parent package.
If you are on npm v6.X it's possible to run scripts on npm install <package> by using Hook Scripts.
Unfortunately it seems the functionality has been removed in newer versions.
In order to do so you need to create a script inside node_modules/.hooks/{eventname} where {eventname} is the event you want to hook on.
For example to hook on postinstall you'll need node_modules/.hooks/postinstall
There's a catch though: it won't run on Windows, because it won't be able to recognize the file as an executable since it's lacking a file extension.
A not so pretty workaround is to create, for instance, a postinstall.cmd and soft (or hard /H) linking it with mklink postinstall postinstall.cmd
This will ensure that Windows recognizes the file as a .cmd executable to correctly run it.
I am trying to add this plugin to my app: https://www.npmjs.com/package/cordova-pdf-generator
I installed NPM as instructed in https://blog.teamtreehouse.com/install-node-js-npm-windows. Then opened command prompt window and went to my app's folder. later i ran below commands in command prompt "npm install cordova-pdf-generator".After that ran this: "cordova plugins add node_modules/cordova-pdf-generator"
While running cordova plugins add node_modules/cordova-pdf-generator i am getting cordova.js script error.Please help me!!
Thanks,
Raghu
Those instructions seem... odd. After installing npm, you should only need to make a call to:
cordova plugin add cordova-pdf-generator --save
This should install the plugin from npm to your app, and add a line to your config.xml with the plugin info.
EDIT: thinking about the npm stuff some more, it looks like the author did some weird stuff to archive it. So... here's one way to get the plugin working. You've probably already called:
npm install cordova-pdf-generator
So that means you should have a subdirectory of the current directory named node_modules. The next steps you need to take:
Create a cordova project:
cordova create MyProject
Add the plugin to the project:
cd MyProject
cordova plugins add ../node_modules/cordova-pdf-generator
The creating of the project was needed for the plugin to be added to it. That step doesn't appear to be listed in the cordova-pdf-generator project on npmjs.com.
I am new to Electron (Atom-shell), and I am trying to load a NodeJS plugin into the application I am building, but I don't know how. The documentation is not clear on that.
For instance, I am trying to use sqlite3 plugin in my app, I used npm install sqlite3, and it was successfully installed. But the application throws and error when I try to call it var sqlite = require('sqlite3'). Are there any further steps I am not aware of ?
Thanks.
For pure JS (i.e. not native) modules you need the following:
Have the module listed in your package.json dependencies
Let electron know where to find the module (e.g. export NODE_PATH=/PATH/TO/node_module)
The first requirement is obvious and the second has its roots in this issue.
For native node modules (such as sqlite3) which use C++ bindings, you need to build them against electron headers to work. According to electron docs, the easiest way to do that would be:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this
./node_modules/.bin/electron-rebuild
To install the npm modules correctly you should go into the folder of your electron app and install the module via npm.
npm install --save sqlite3
The flag --save is important, because npm will install the module inside your app.
Afterwards the require should work.
I am playing with a node module called filings
when I first used it, I did a git clone to get that project into a local directory, where I ran npm install and then grunt to build all of the coffeescripts into JS and get dependencies installed.
That works fine and I end up with a /lib directory with 13 files in it.
Now, i'm trying to use that module within a separate node project, so I installed it with npm install filings and a node_modules/filings/lib directory was created, but it only has 6 files in it, and a bunch of functionality is missing.
I've never installed a coffeescript node module into a regular node project before, is there something I can do to get it to properly build within my project?
This is a problem with the package on npmjs.org: If we clone a module from the repository, install it, and make package with npm pack then we get a very different contents of the package to install than the package manager on the site:
http://registry.npmjs.org/filings/-/filings-0.2.0.tgz
coffee-script require (generally spoken)
you can require coffee-script files from a javascript-file if you use coffee-script/register before.
therefore you need to install coffee-script locally:
$ npm install coffee-script --save
and use it in your code:
require('coffee-script/register');
require('youre-coffee-script-module');
using the filings-module
basic setup of filings
the module itself has its main-script set to ./lib/filings.js
the lib-folder is compiled by the grunt-task publish
the publish-Task itself is triggered via the prepublish-script. so anytime the package-auther publishes his package via npm publish the lib-sources are build, an afterwards the compiled package is published.
in the .npmignore file all the source and testfiles are ignore, and therefore not published to npm!
how to fix your problem
i just see 2 more or less clean possibilities:
open an issue, and ask the package-auther to publish a new version to npm
create a fork, and use the fork
a) fork the repo
b1) add a postinstall-Property which runs the grunt-default-task grunt:
"scripts": {
"postinstall": "grunt"
}
or b2) create a index.js file where you require the coffee-sources like i pointed out above
c) install the package from your repo: npm install git+ssh://git#github.com:YOURGITHUBUSERNAME/filings.git
d) maybe create a pull-request if you do version b2
I was hoping that grunt-contrib-coffee could work with blackcoffee but it seems that it uses the coffee-script npm module directly instead of calling the coffee executable which would have invoked blackcoffee. I have looked around to see if there are any pre-existing Grunt plugins for blackcoffee but this doesn't seem to be the case.
Any recommendations as to how to go about configuring Grunt so that grunt watch will let me compile my blackcoffee code?
don't know if that works, but i think it should:
fork grunt-contrib-coffee
rename it in your package.json to grunt-blackcoffee
replace coffee-script dependency, and replace with blackcoffee in your package.json
search for the coffee-script require in the grunt task, currently it's here:
return require('coffee-script').compile(code, coffeeOptions);
and replace it with
return require('blackcoffee').compile(code, coffeeOptions);
publish it to npm
if you don't want to publish to npm, you can install packages from git:
$ npm install git+ssh://your-git-repo.git --save