I want to run an index.js file as a part of my gulp process. I could add node index.js through something like gulp-shell as described in this thread (How to run bash commands in gulp?) but it seems there should be an easier way.
I figured there would be a plugin called gulp-node or something to do this but it doesn't exist. How do others do it without turning your initial script into a gulp plugin, which seems intrusive?
You can use var data = require('./path/to/index') to load/run the index.js module in your Gulp task and send any exported information to data. You can read more about require and Node.js modules here.
Related
I recently discovered that, whenever I try to to execute a node script like that:
"run-my-script": "node ./folder/function.js executeFunction $ARG"
It seem that using a script with node causes troubles with ES6 imports.
If I run a basic script like that I get this error:
SyntaxError: Cannot use import statement outside a module
My running command line look like that:
ARG=myArg npm run run-my-script
Like if it was impossible for meteor to run a node script outside without the need to transpile code to ES6 first.
All my modules are designed by ES6 modules approach:
import { SchemaBuyers } from './schema';
I don't want to specify type=module inside my package.json because first of all, it's not necessary, I don't have compilation errors when building the apps, ony when running pure node script, and secondly it would require to rename all my files extentions to .mjs for example.
Thank you very much !
You can use node.js's --input-type option for this if you pipe the file into it:
cat test.js | node --input-type=module
Or in your case:
"run-my-script": "cat ./folder/function.js | node --input-type=module executeFunction $ARG"
This assumes that the code in function.js will parse the provided arguments.
I have a nodejs project written in Typescript. Therefore, i have webpack using a typescript loader that transpiles my code in Javascript and bundles it in a server.js file (in a dist folder)
When in developement conditions, my webpack runs with its watcher ON and so does nodemon.
Problem is, when i launch my script for the first time combining webpack and nodemon, since webpack is in watch mode it doesn't have an exit code saying that everything is ok, nodemon script can be started. If i run them simultaneously, nodemon will launch faster than webpack and since server.js file doesn't yet exist, it will crash at the start.
I want to launch thanks to one single command both scripts but make nodemon command wait for the bundling to be done.
First of all, when please provide some code when submitting questions.
and since server.js file doesn't yet exist
I think you should work around your setup a little bit s.t. webpack doesn't create your server.js file if you want to do this.
Basically you can chain multiple commands in a script like so webpack -d && nodemon index.js. This will launch node after webpack completes. However if you setup webpack in watch mode -w it never exists, so you can't chain another command to it. So webpack -d -w && nodemon index.js never gets to the nodemon part.
A solution to the above is to chain them using only &, which I guess you are doing, but in this way they both start at the same time. Hence, if you make your setup independent (webpack doesn't interfere with nodemon starting script) you can list them like so.
If for whatever reasons you can't do this or don't want to, your only option is with 2 separate scripts that you launch manually one after the other.
If I were you, I would just use nodemon-webpack-plugin:
Uses Nodemon to watch and restart your module's output file, but only
when webpack is in watch mode (ie, --watch).
Saves the need for installing, configuring and running Nodemon as a
seperate process.
I have a new project where I'm using browserify to convert node modules into an sdk that can run inside the browser.
I'm requiring a number of other npm packages like:
var log4js = require('log4js');
That run fine and give me no problems in the browser, however JsSip just will not cooperate. When I do
var JsSIP = require('jssip');
I get
plivowebsdk.js:2 Uncaught Error: Cannot find module '../../package.json'
Looking through the code, it's obvious when it makes this call
var pkg = require('../../package.json');
is where it bombs out. Clearly it cannot find the package.json file, which it uses to pull out version information. I know JsSip is actually built with browersify itself (or used to be) so that it can run in either node or a browser. Is this causing a conflict?
Still sort of new to browserify, is their a configuration option or transformation I can perform to get around this?
Turned out the be browserify errors, re did the build process using the gulp recipes for browersify and works as expected.
Having started to work on node.js recently, I am trying to use rename package. Successfully installed (npm install rename) package and sample code (residing in file test.js) is pasted below:
console.log('Starting to rename');
var rename = require('rename');
rename('a.js', 'b.js');
console.log('Rename done');
The file a.js resides in the same directory as test.js. The console shows the two debug messages correctly, but the file a.js is not renamed.
How can I enable debug on rename module (or for that matter any node module), so that I can debug it additional log messages?
Note: Other packages like find, mkdirp, etc are working fine.
You can enable debug by doing a DEBUG=* node app.js or passign env variable DEBUG with wildcard * . It would enable debug for all modules, provided the module you wish to debug is actually using debug.
Hardcore way of doing is to make local changes in the module.
I was thinking it would be handy if I could reload! code in the Node console, something to the effect of:
reload('./path/to/my/file.js')
that would delete the code from the cache then load it again -- handy for exercising prototype code. Seemed like a natural function to place in node's equivalent of ~/.irbrc. But I can't find that. Does node have such a thing?
I don't know there's such a tool. But I believe what you need can be achieved by using nodemon.
Use sudo npm install nodemon -g to install it. Then launch your application by nodemon app.js instead of node app.js. Then it watches for changes to nearby .js files, and automatically restart when you save.