Node JS - Custom command to run app.js - node.js

I want to be able to install my package through npm and then run app.js with a custom command without using npm start. I have looked around the web and many examples show adding a line in scripts in the package.json file, but all of these require it to be run using npm start. Is there a way to execute a .js file with node using a custom command e.g. importer --file?

Before publishing your package, you'll need to have a package.json file with at least the following:
{
"name": "importer",
"version": "0.0.1",
"bin": "./importer.js"
}

Related

Create pseudo node_modules folder on npm install

I updated some libraries within my project and now I get an issue with the zlib within ng2-pdf-viewer.
There is also an issue created on github, that helped to fix it for now:
https://github.com/VadimDez/ng2-pdf-viewer/issues/322#issuecomment-389281242
The solution suggests to create a zlib folder within node_modules and add an empty index.js and a basic package.json.
This fixed the issue. But now I want to automate the process, since every time anyone runs npm install the pseudo folder is gone again.
Is there any way to create this folder with it's content automatically on npm install (maybe with mkdir or something)?
PS: The solution to put "browser": { "zlib": false } into the package.json didn't help in my case...
You should be able to create a script to create the directory, and then use a hook to run that script after you run npm install:
"scripts": {
"postinstall": "myCustomScriptToCreateFolder",
},
More reading:
npm Scripts Documentation
StackOverflow Post about this sort of thing

Set up Socket.IO to run on Node Windows

I am new to programming and I am doing the WebRTC codelab here: https://codelabs.developers.google.com/codelabs/webrtc-web/#6
I am at the part titled "Set up Socket.IO to run on Node" where it says "To install dependencies, run the following from the command line terminal in your work directory:
npm install", however, when I type npm install the command prompt in Windows, it does not recognize the command. I feel like this should be pretty easy, so am I missing something?
Set up Socket.IO to run on Node
For this and the following steps, you'll run Socket.IO on Node.
At the top level of your work directory create a file named package.json with the following contents:
{
"name": "webrtc-codelab",
"version": "0.0.1",
"description": "WebRTC codelab",
"dependencies": {
"node-static": "0.7.7",
"socket.io": "1.2.0"
}
}
This is an app manifest that tells Node Package Manager (npm) what project dependencies to install.
To install dependencies, run the following from the command line terminal in your work directory:
npm install
You should see an installation log that ends something like this:
enter image description here

How to make npm scripts refer to local packages?

I am working on a project and I want to use JSdoc for documentation. I have listed it in devDependencies and created a script to run it. My package.jsonlooks like:
"scripts": {
"doc": "jsdoc -c ./conf.json"
},
"devDependencies": {
"jsdoc": "^3.4.3"
}
Cloning my project on another machine and typing npm install installs jsdoc to the node_modules folder.
However, when I run npm run doc, I get a large error, the center of which is: 'jsdoc' is not recognized as an internal or external command
This is because JSdoc is installed in node_modules not my local path.
What is the best way to point npm-run commands to my locally installed modules? Also, what is the point of having dependencies such as JSdoc listed in devDependencies (thus installed in node_modules) when they can not be used from there?

NPM: Change the `bin` output directory for the node modules

Currently, if you are using a package.json file to manage your project's dependencies (whatever project it is, may it be a ruby, php, python or js app), by default everything is installed under ./node_modules.
When some dependencies have binaries to save, they're installed under ./node_modules/.bin.
What I need is a feature that allow me to change the ./node_modules/.bin directory for ./bin.
Simple example:
A PHP/Symfony app has a ./vendor dir for Composer dependencies, and all binaries are saved in ./bin, thanks to the config: { bin-dir: bin } option in composer.json.
But if I want to use Gulp to manage my assets, I create a package.json file, require all my dependencies and then run npm install.
Then, my wish is to run bin/gulp to execute gulp, but actually I have to run node_modules/.bin/gulp which is not as friendly as bin/gulp.
I've looked at package.json examples/guides on browsenpm.org and docs.npmjs.com, but none of them works, because they are here to define your own project's binaries. But I don't have any binaries, because I want to use binaries from other libraries.
Is there an option for that with NodeJS/NPM ?
You might consider adding gulp tasks to your package.json.
// package.json
{
"scripts": {
"build-templates": "gulp build-templates",
"minify-js": "gulp minify-js"
}
}
You can run any scripts specified in package.json by simply running the following:
$ npm run build-templates
$ npm run minify-js
You get the idea. You can use the gulp command inside the string without doing ./node_modules/.bin/gulp because npm is smart enough to put all scripts from ./node_modules/.bin/ into the path for that script execution.

Create WebStorm run configurations from package.json "scripts" section

In my package.json file, I have the following "scripts" configuration.
...
"scripts": {
"start": "watchify -o lib/index.js -v -d .",
"build": "browserify . | uglifyjs -cm > lib/index.js",
"test": "jest"
}
...
This allows me to run npm start, npm build and npm test from the command line.
This is great! But ideally, I would like to be able to run those tasks from within WebStorm using run configurations, due to how convenient the interface is. I have not been able to figure out how to do this.
Is there a way to create my own custom run configurations or automatically generate them from my package.json?
you can use Node.js Run configuration for this. For example, for 'npm start':
Working dir: /path/to/your/package.json
JavaScript file: /path/to/global/node_modules/npm/bin/npm-cli.js
Application parameters: run start
To find the global node_modules path from the command line use "npm root -g".
There is no way to auto-create run configurations from files. And the only way to create your own run configuration is developing a plugin - see http://confluence.jetbrains.com/display/IDEADEV/Run+Configurations
Update: since 2016.x, WebStorm provides a special run configuration - npm - for running/debugging NPM scripts. It can be created manually via Edit configurations... dialog, or auto-added by selecting the script in NPM tool window (can be opened from package.json right-click menu).
See https://www.jetbrains.com/help/webstorm/2017.3/running-npm-scripts.html
WebStorm and IntelliJ 2016 included support for NPM scripts as part of the NodeJS plugin.
Scripts are launched in four ways:
From a tree of scripts in the dedicated NPM Tool Window. The tool window opens when you invoke npm by choosing Show npm Scripts on the context menu of a package.json in the Project tool window or of a package.json opened in the editor.
According to a dedicated run configuration, see Run/Debug Configuration: NPM.
Automatically, as a start-up task.
As a before-launch task, from another run configuration.
For more details check out their documentation.

Resources