Browserify and bower. Canonical approach - node.js

The way I'm using packages that not available out of the box in npm, right now is like that:
package.json has:
"napa": {
"angular": "angular/bower-angular",
"angular-animate": "angular/bower-angular-animate",
"d3": "mbostock/d3",
"ui-router":"angular-ui/ui-router",
"bootstrap":"twbs/bootstrap"
},
"scripts": {
"install": "node node_modules/napa/bin/napa"
and that installs files into node_modules directory, and I use them natively like this
require('angular/angular')
require('ui-router')
... etc
That works, but I was thinking if it's possible to use packages installed with bower (into bower specific folder) and use them natively as node modules? Is it possible to tweak node's module resolution and force it to look for modules not just inside node_modules directory, but also in bower directory as well? Or maybe using npm link or whatever?
Is there some sort of convention to use browserify with bower?

You can try to install via debowerify
The package.json may then look as follows:
{
"name": "browserify-begin",
"version": "0.0.0",
"dependencies": {
},
"browserify": {
"transform": [
"debowerify"
]
},
"devDependencies": {
"browserify": "^4.1.5",
"debowerify": "^0.7.1",
"grunt": "^0.4.5"
}
}
Given angular is installed with
bower install angular
Then within the js file will be as follows:
require("angular");

You can use browserify-shim and configure the bower-installed modules in your package.json like this:
"browser": {
"angular": "./bower_components/angular/angular.js",
"angular-resource": "./bower_components/angular-resource/angular-resource.js"
},
"browserify-shim": {
"angular": {
"exports": "angular"
},
"angular-resource": {
"depends": ["./bower_components/angular/angular.js:angular"]
}
},
Then your code can require them by their short name as if there were regular npm modules.
Here is the spec for the "browser" package.json property.

Related

npm link not working for one of four local packages

I have 4 packages in my package.json:
"#CT/package-1": "*",
"#CT/package-2": "*",
"#CT/package-3": "*",
"#CT/package-4": "*",
All four packages are not on the NPM registry, but locally linked.
In each package directory I did:
sudo npm link
After that I went into my main project and linked all packages like so:
npm link #CT/package-1
npm link #CT/package-2
npm link #CT/package-3
npm link #CT/package-4
For each command it outputs the linked chain of the sym-links, I checked the paths of each one, and it's correct. If I go in the node_modules folder of my main project and follow #CT/package-X I will end up in my local package directory.
Now I expect that I can run npm i on my main project and it won't try to install those packages from the npm registry. This works for package 2-4, but it does not work for package-1:
npm ERR! code E404
npm ERR! 404 Not Found - GET https://npm.pkg.github.com/#CT%2fpackage-1 - npm package "package-1" does not exist under owner "CT"
As soon as I remove "#CT/package-1": "*", from my package.json, npm i will success, because it seems to right-fully skip package 2-4, just not package-1.
Now I inspected all four package projects, and it makes no sense to me. All projects compile, all package.json contain the correct package name. Here is the package.json of my first faulty package:
{
"name": "#CT/package-1",
"version": "0.0.2",
"description": "",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"unpkg": "dist/plugin.js",
"files": [
"android/src/main/",
"android/build.gradle",
"dist/"
],
"author": "martin-braun",
"license": "ISC",
"keywords": [
"capacitor",
"plugin",
"native"
],
"scripts": {
"lint": "npm run prettier -- --check",
"prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
"build": "npm run prettier && npm run clean && tsc && rollup -c rollup.config.js",
"clean": "rimraf ./dist",
"watch": "tsc --watch",
"prepublishOnly": "npm run build"
},
"devDependencies": {
"#capacitor/android": "^3.0.0",
"#capacitor/core": "^3.0.0",
"#capacitor/docgen": "^0.0.18",
"#ionic/eslint-config": "^0.3.0",
"#ionic/prettier-config": "^1.0.1",
"eslint": "^7.11.0",
"prettier": "~2.2.0",
"prettier-plugin-java": "~1.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.32.0",
"typescript": "~4.0.3"
},
"peerDependencies": {
"#capacitor/core": "^3.0.0"
},
"prettier": "#ionic/prettier-config",
"eslintConfig": {
"extends": "#ionic/eslint-config/recommended"
},
"capacitor": {
"android": {
"src": "android"
}
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/CT"
},
"dependencies": {}
}
.npmrc
registry=https://npm.pkg.github.com/#CT
registry=https://registry.npmjs.org
When I linked the project on my main project it shew the right paths (i.e.)
/Users/mb/Projects/CT/MAIN/node_modules/#CT/package-1 -> /Users/mb/node_module_collections/ekol/lib/node_modules/#CT/package-1 -> /Users/mb/Projects/CT/package-1
What could I try to find the issue? Again, package 2-4 won't cause npm i to fail, but package.json and npmrc look similar. I'm thankful for any advice.
node v13.12.0

Issue Building Electron App with Opencv4nodejs on Windows 10

I have built an electron app that uses tensorflow and opencv4nodejs.
The app works well when I use npm start. However, when I try to build my app, it bombs out with the following error:
The issue here is that I have not built the opencv4nodejs from source. Instead I manually installed opencv and then disabled autobuild to install it for my app.
From the error, I see that it is looking for a lib directory under opencv-build but it is simply not there.
Is there a way to NOT have anything rebuilt since I already have all the modules built for Windows 10?
Am sure this is something simple but have run up against a brick wall for this.
Here is my package.json
`
{
"name": "myApp",
"version": "0.0.1",
"description": "My Electron App",
"main": "main.js",
"license": "abc",
"private": true,
"scripts": {
"postinstall": "install-app-deps",
"start": "electron .",
"build": "electron-packager . nGage --platform win32 --arch x64 --out dist/ --icon image/nGage-Icon.ico --overwrite",
"setup": "electron-installer-windows --src dist/nGage-win32-x64/ --dest dist/installers/ --config config.json --overwrite",
"dist": "build"
},
"build": {
"appId": "com.electron.app",
"publish": [
{
"provider": "generic",
"url": "abc"
}
],
"win": {
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
}
],
"certificateFile": "cert/abc",
"certificatePassword": "xyz"
},
"asar": false,
"nsis": {
"oneClick": true,
"perMachine": false,
"artifactName": "${productName}-Setup-${version}-x64.${ext}"
}
},
"author": {
"name": "ABC",
"email": "xyz#abc.com",
"url": "www.abc.com"
},
"devDependencies": {
"electron": "^9.0.3",
"electron-builder": "^19.53.6",
"electron-installer-windows": "^0.2.0",
"electron-packager": "^8.5.2",
"electron-winstaller": "^2.5.2",
"grunt-electron-installer": "^2.1.0"
},
"dependencies": {
"#tensorflow/tfjs": "^2.0.0",
"#tensorflow/tfjs-node": "^2.0.0",
"auto-launch": "^5.0.1",
"cron": "^1.2.1",
"electron-config": "^0.2.1",
"electron-positioner": "^3.0.0",
"electron-squirrel-startup": "^1.0.0",
"electron-updater": "^2.19.0",
"electron-window": "^0.8.1",
"graceful-fs": "^4.1.11",
"homedir": "^0.6.0",
"https": "^1.0.0",
"opencv4nodejs": "^5.6.0",
"request": "^2.88.2",
"url": "^0.11.0",
"username": "^3.0.0",
"util": "^0.12.3",
"windows-build-tools": "^5.2.2"
}
}
`
Any insights will be most helpful!
Thanks,
Arun
So I realised that I needed to set the following environment variables before doing npm run build
since I had used them while build opencv4nodejs. Here is what I have taken from the the opencv4nodejs site.
`
Installing OpenCV Manually
Setting up OpenCV on your own will require you to set an environment variable to prevent the auto build script to run:
# linux and osx:
export OPENCV4NODEJS_DISABLE_AUTOBUILD=1
# on windows:
set OPENCV4NODEJS_DISABLE_AUTOBUILD=1
Windows
You can install any of the OpenCV 3 or OpenCV 4 releases manually or via the Chocolatey package manager:
# to install OpenCV 4.1.0
choco install OpenCV -y -version 4.1.0
Note, this will come without contrib modules. To install OpenCV under windows with contrib modules you have to build the library from source or you can use the auto build script.
Before installing opencv4nodejs with an own installation of OpenCV you need to expose the following environment variables:
OPENCV_INCLUDE_DIR pointing to the directory with the subfolder opencv2 containing the header files
OPENCV_LIB_DIR pointing to the lib directory containing the OpenCV .lib files
Also you will need to add the OpenCV binaries to your system path:
add an environment variable OPENCV_BIN_DIR pointing to the binary directory containing the OpenCV .dll files
append ;%OPENCV_BIN_DIR%; to your system path variable
`

webpack terminal command results in /config/webpack/development.js not found

When I enter $ webpack into my terminal I receive the following error:
webpack config
/Users/kristenmkulha/Desktop/react-help-queue/config/webpack/development.js
not found, please run 'bundle exec rails webpacker:install' to install
Webpacker with default configs or add the missing config file for your
custom environment.
I found a similar issue here: Webpack command in node brings /config/webpack/development.js not found and changed the deprecated babel-preset-es2015 to babel-preset-env and I already had the -loader suffix. These updates still result in the same error.
Here is my webpack.config.js:
const webpack = require('webpack');
const { resolve } = require('path');
module.exports = {
entry: [
resolve(__dirname, "src") + "/index.jsx"
],
output: {
filename: 'app.bundle.js',
path: resolve(__dirname, 'build')
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: "babel-loader",
options: {
presets: [
"env",
"react"
]
}
},
],
}
};
Here is my package.json file:
{
"name": "react-help-queue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"webpack": "^3.8.1"
}
}
Huh, it looks like running "webpack" on the command line is invoking a different tool (maybe "webpacker"? https://github.com/rails/webpacker)
There are two ways to solve this:
add a additional script to package.json to kick off webpack.
"scripts": {
"build": "webpack -p"
},
With the above added, execute this command in a terminal: npm run build.
When a script is executed via npm run, npm will automatically include binaries (like webpack) in its PATH, so it the script uses the webpack binary that is part of the apps dev dependencies. Read more on this here: https://docs.npmjs.com/cli/run-script.
Install webpack globally: npm install -g webpack.
This will make webpack available at the command line in all cases, which allows the developer to just type webpack and have it run. This should(?) override the existing "webpacker" command. If not, you may need to uninstall that tool or modify your PATH.
Installing npm packages globally is convienient but it can cause headaches. For example, if two different projects need two different versions of webpack, it may not be efficient. Also, you may find there are several steps in a "build" to execute once an application gets more complex, so having a convenient script included in the project may be appealing (especially for projects with multiple developers).
Hope this helps!

Generating a bin folder in NodeJS with Fedora

I'm learning to do CRUD in Node, and am having trouble generating the bin folder. Also, the package.json folder which I've generated doesn't contain the dependencies needed to run on localhost.
To generate the app, I used express MyApp
and then npm install npm install mongoose --save
This gave me:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.5.2",
"jade": "*",
"mongoose": "^4.5.4"
}
}
It did not generate a bin folder, or www file hiding elsewhere. I'm using Fedora 23, in case that makes a difference here. Please let me know if there's something I've missed, or if you have any suggested work-arounds.

npm install error for openshift node.js project

When deploying an OpenShift node.js project with a depdency on browserify 4.0.0, I get an error installing browserify's dependencies. Specifically:
...
remote: npm ERR! Error: No compatible version found: stream-browserify#'^1.0.0'
remote: npm ERR! Valid install targets:
remote: npm ERR! ["0.0.0","0.0.1","0.0.2","0.0.3","0.0.4","0.1.0","0.1.1","0.1.2","0.1.3","1.0.0"]
...
Given that stream-browserify's version is ^1.0.0 according to browserify's depdency and that openshift is suggesting 1.0.0 is a valid install target, why is this failing? I have seen this error in other cases, whenever the highest available openshift version fits the careted package.json version.
Am I misunderstanding what the caret means? Is this an OpenShift bug?
My package.json:
{
"name": "SampleApp",
"version": "1.0.0",
"description": "do things online",
"keywords": [
"OpenShift",
"Node.js",
"application",
"openshift"
],
"author": {
"name": "J",
"email": "j#email.com",
"url": ""
},
"homepage": "http://www.openshift.com/",
"repository": {
"type": "git",
"url": "https://github.com/openshift/origin-server"
},
"engines": {
"node": "0.x",
"npm": "1.x"
},
"dependencies": {
"body-parser": "1.x",
"browserify": "4.0.0",
"cookie-parser": "1.x",
"cookie-session": "1.x",
"express": "4.x",
"fast-csv": "0.x",
"multer": "0.0.5",
"pg": "3.x",
"sql": "0.x",
"xlsx-extract": "0.0.4"
},
"devDependencies": {
},
"bundleDependencies": [],
"private": true,
"main": "server.js",
"scripts": {
"build-js": "browserify public/index.js -o public/index-bundle.js & browserify public/intake.js -o public/intake-bundle.js",
"start": "npm run build-js && node server.js"
}
}
This behaviour could be because of different versions on node and npm on your local machine and openshift environment. Start by fixing "engines" attribute in your package.json, something as below:
"engines": {
"node": ">= 0.10",
"npm": ">= 1.4"
}
If still the issue is there (on openshift) it is due to the unavailability of the nodejs/npm required versions on openshift environment. For example, as of today, on my local machine I may be using node version 0.10.28 and npm version 1.4.9, but on openshift nodejs default cartridge I have to be content with nodejs version 0.10.5 and npm version 1.2.17, which is a big gap.
So, in this case, the easiest way to get around is by using "npm shrinkwrap", which freezes the nested dependency versions that ought to be used, hence doing away with varied behaviour of npm versions to figure out the nested dependency to install.
Can read about shrinkwrap here: https://www.npmjs.org/doc/cli/npm-shrinkwrap.html
So, on your local machine:
run npm install and make sure everything works.
fire npm shrinkwrap This will create a file - "npm-shrinkwrap.json", with the required shrinkwrap info. Add, commit and push the file to the openshift git repo.

Resources