How do I use nodemon's --inspect option correctly? - node.js

Preface: Attempting to write a discord bot and I have no experience with Node and a small amount with javascript with HTML.
When I input nodemon --inspect Version 1.0.js into powershell it just gives me this as the return message:
Usage: nodemon [nodemon options] [script.js] [args]
See "nodemon --help" for more.
I am getting most of my starting resources from this How-To Geek article: https://www.howtogeek.com/364225/how-to-make-your-own-discord-bot/. I don't know how reputible they are when it comes to coding but I thought I'd try it.

From the docs:
If no script is given, nodemon will test for a package.json file and
if found, will run the file associated with the main property (ref).
You can also pass the inspect flag to node through the command line as
you would normally:
nodemon --inspect ./server.js 80

Related

How to run node.js cli with experimental-specifier-resolution=node?

Our team has built a small CLI used for maintenance. The package.json specifies a path for with the bin property, and everything works great; "bin": { "eddy": "./dist/src/cli/entry.js"}
Autocompletion is achived by using yargs#17.0.1. However we recently converted the project to use es6 modules, because of a migration to Sveltekit, i.e. the package.json now contains type: module. Because of this, the CLI now only works if we run with:
what works
node --experimental-specifier-resolution=node ./dist/src/cli/entry.js help
However, if we run this without the flag, we get an error "module not found":
Error [ERR_MODULE_NOT_FOUND]: Cannot find module...
So the question is
Can we somehow "always" add the experimental-specifier-resolution=node to the CLI - so we can continue to use the shorthand eddy, and utilize auto completion?
There are two probable solutions here.
Solution 1
Your entry.js file should start with a shebang like #!/usr/bin/env node. You cannot specify the flag directly here, however, if you could provide the absolute path to node directly in the shebang, you can specify the flag.
Assuming you have node installed in /usr/bin/node, you can write the shebang in entry.js like:
#!/usr/bin/node --experimental-specifier-resolution=node
(Use which node to find the absolute path)
However, this is not a very portable solution. You cannot always assume everyone has node installed in the same path. Also some may use nvm to manage versions and can have multiple version in different path. This is the reason why we use /usr/bin/env to find the required node installation in the first place. This leads to the second solution.
Solution 2
You can create a shell script that would intern call the cli entry point with the required flags. This shell script can be specified in the package.json bin section.
The shell script (entry.sh) should look like:
#!/usr/bin/env bash
/usr/bin/env node --experimental-specifier-resolution=node ./entry.js "$#"
Then, in your package.json, replace bin with:
"bin": { "eddy": "./dist/src/cli/entry.sh"}
So when you run eddy, it will run the entry.js using node with the required flag. The "$#" in the command will be replaced by any arguments that you pass to eddy.
So eddy help will translate to /usr/bin/env node --experimental-specifier-resolution=node ./entry.js help
Just add a script to your package.json:Assuming index.js is your entry point and package.json is in the same directory
{
"scripts": {
"start": "node --experimental-specifier-resolution=node index.js"
}
}
Then you can just run on your console:
npm start

Mysterious parameter -r in Nodemon

I've been searching on how to reload env on the development environment while using Nodemon.
The command that spread all over on the internet is nodemon -r dotenv/config bin/www.js, this is actually working, but look at the command, what is the -r thing? I'm out of curiosity.
I've been searching on the official documentation with no luck. https://github.com/remy/nodemon/wiki
Anyone care to explain, thank you.
I finally found an answer.
Quoted from Nodemon docs:
nodemon wraps your application, so you can pass all the arguments you would normally pass to your app
https://github.com/remy/nodemon#usage
So basically any arguments that unknown to Nodemon will redirect to Node.js app, so -r is passed to the Node.js, which means "Require module".
See: https://nodejs.org/dist/latest-v12.x/docs/api/cli.html#cli_r_require_module
-r is not a valid argument option
Take a look at
https://github.com/remy/nodemon/blob/master/lib/cli/parse.js
for all supported options

How to use source maps in node js?

I stareted my node app with node index.js and got the following message:
(node:10128) UnhandledPromiseRejectionWarning: TypeError: e.reduce is not a function
at Module.te (C:\Projects\myproject\node_modules\tronweb\dist\TronWeb.node.js:1:9236)
Now I'm interessted in whats happening. I've seen that there are mapping files TronWeb.node.js.map in the tronweb\dist directory. I started again using --inspect and opened the chrome dev tools. But in the console I see exactly the same message.
In Node v12.12.0+ sourcemaps are supported natively. Pass --enable-source-maps flag to enable them.
One caveat in Node v12.12.0 is that Error.prepareStackTrace will no longer be called when source maps are enabled. This was fixed in v12.16+.
You can use https://www.npmjs.com/package/source-map-support
$ npm install source-map-support --save-dev
Then change your run command in package.json to:
node -r source-map-support/register index.js
(Note that you should have an index.map.js next to index.js)

Node `--inspect` flag with Coffeescript

Node has been supporting the --inspect debug flag which activates the Google DevTools for the given process. Is there a way to get that flag to work with Coffeescript through the coffee command? I tried using the --nodejs argument but it doesn't give me a debug link.
This can be done by calling node with the --inspect flag and passing the path to the coffee executable as the next argument.
eg. node --inspect-brk $(which coffee) ./test.coffee

How to run babel-node in deattach mode

I follow this tutorial.
It uses NODE_ENV=production node_modules/.bin/babel-node --presets 'react,es2015' src/server.js
My 1st question is:
How to I run the command line above in deattach mode? i.e. the command line above doesn't allow me to quit the terminal.
My 2nd question is:
However, from the docs (https://babeljs.io/docs/usage/..., it is said that you shouldn't use babel-node in production, as it is very heavy. So what is the proper way tot run the server?

Resources