TLDR and simple
Use the npm package: cordova-import-npm
Long question and DIY
You can use a hook before cordova prepare, that is, when the files are assembled for compiling.
- Edit your
config.xml and add this line (not inside any platform but in the root, i.e. inside <widget):
<hook src="hooks/importNpmPackages.js" type="before_prepare"/>
- Create the file
hooks/importNpmPackages.js with this content
const fse = require('fs-extra')
const path = require('path')
const twoSpaces = ' ' // for log indentation
var projectRoot
module.exports = function (context) {
console.log(`${context.hook} : ${path.relative(context.opts.projectRoot, context.scriptLocation)}`)
projectRoot = context.opts.projectRoot
console.log(twoSpaces + 'Project root directory: ' + projectRoot)
copyFile('jquery', path.join('dist', 'jquery.min.js'), path.join('js', 'res', 'jquery.min.js'))
copyFile('bootstrap', path.join('dist', 'js', 'bootstrap.min.js'), path.join('js', 'res', 'bootstrap.min.js'))
copyFile('bootstrap', path.join('dist', 'css', 'bootstrap.min.css'), path.join('css', 'res', 'bootstrap.min.css'))
}
function copyFile (npmPackage, // oficial name of the npm package from which the file is to be copied from
fileRelativePath, // file path with respect to the main directory of the npm package (node_modules/<package>/)
destFilePath) { // file's path to where it is copied, relative to the project www/ directory
const packageDirFullpath = path.dirname(require.resolve(path.join(npmPackage, 'package.json')))
const fileOriginFullPath = path.join(packageDirFullpath, fileRelativePath)
const fileDestFullPath = path.join(projectRoot, 'www', destFilePath)
fse.copySync(fileOriginFullPath, fileDestFullPath)
const consoleMsg = npmPackage + ': ' +
path.relative(projectRoot, fileOriginFullPath) + ' -> ' +
path.relative(projectRoot, fileDestFullPath)
console.log(twoSpaces + consoleMsg)
}
As you can see in this file I am copying jquery and bootsrap files using a function copyFile with this syntax:
copyFile(
'<npmPackageName>',
'<path/of/originFile/relative/to/packageDir>',
'<path/to/destFile/relative/to/wwwDir>'
)
I use this in all my cordova projects and it works like a charm. That's how I see it

The hook is also triggered upon cordova build because cordova build equals to cordova prepare && cordova compile.