How to run Nodemon with TypeScript in debug mode in WebStorm - node.js

I have a very basic Express server setup with NodeJS/Express + Nodemon. I'm using WebStorm as my IDE.
When running the debug/inspect script, my application is throwing a compile-time error. Following the instructions here, and cognizant of the post here, my (simplified) package.json looks like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.ts",
"debug": "nodemon --inspect=127.0.0.1:9229 src/app.ts"
},
"devDependencies": {
"#types/express": "^4.17.9",
"#types/node": "^14.14.19",
"nodemon": "^2.0.6",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
When I run npm run debug I receive the following error:
I've also tried the following, all with the same result:
"debug": "nodemon --inspect src/app.ts"
"debug": "nodemon --inspect=0.0.0.0:9229 src/app.ts"
Any ideas why this error is occurring?
The command npm run dev does not generate an error. Upon further investigation, nodemon automatically substitutes ts-node when targeting a .ts file. Ts-node, in turn, recommends debugging by registering ts-node and running the server with the node command.
If you need to use advanced node.js CLI arguments (e.g. --inspect),
use them with node -r ts-node/register instead of the ts-node CLI.

You can modify your script as follows:
"debug": "nodemon --exec \"node --inspect-brk=0.0.0.0:9229 --require ts-node/register src/app.ts\""
to make sure that the --inspect-brk is passed to Node.js and not to ts-node

Related

Mocha and ts-node gives 'Unsupported Args'

In my package.json I have defined my test script:
"scripts": {
"test": "mocha --require ts-node/register ./test/**/*.ts",
"build": "npx tsc"
}
When I run npm test I get back the result:
> mocha --require ts-node/register ./test/**/*.ts
error: Unsupported Args: --require ts-node/register ./test/**/*.ts
It seems to somehow be the command interaction. Even if I run it manually node .\node_modules\mocha\bin\mocha --require ts-node/register "./test/**/*.ts" it fails with the same message. If I remove --require ts-node/register it runs, but fails when running the test on import statements because my test files are Typescript files.
How do I make mocha work with ts-node?
My package.json file has it in scripts
"scripts": {
"test": "./node_modules/mocha/bin/mocha -r ts-node/register tests/test.ts"
},
And after this I can run npm test and everything seems good.
Versions in devDependencies:
"devDependencies": {
"#types/chai-http": "^4.2.0",
"#types/expect": "^24.3.0",
"#types/mocha": "^9.0.0",
"#types/chai": "^4.2.18",
"#types/node": "14.14.30",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"ts-node": "^9.1.1",
"typescript": "^4.4.4"
}
I was unable to find an answer to how to solve it with the command line, but I circumvented the problem by moving the options into package.json
"scripts": {
"test": "mocha"
},
"mocha": {
"extension": ["ts"],
"spec": "test/**/*.ts",
"require": "ts-node/register"
}
Update: I found the error. I was moving some code without test to having test. The logger was defined in a main.ts file which had some code that ran that was not behind any guard. In fact the error Unsupported Args was my own error message.
The act of referencing a winston logger caused the code in main.ts to run. For some reason this error does not happen now that the mocha options are in package.json even though the logger is still referenced which I find confusing. I am not sure why whether the options are directly in the command line, or they are in the package.json causes such a behavior difference. Regardless, the lesson learned is never reference into your main "running" module.

build before restarting the node.js (express) server using Nodemon

I am using nodemon to watch the changes to the server files and restart the server. It works fine when the script is ec5
nodemon ./server.js
but I want to write the script in ec6 and compile using babel doing the same thing but building before the server is restarted.
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src --out-dir dist",
"serve": "npm run build && node dist/index.js",
"serve-dev": "nodemon dist/index.js"
},
"dependencies": {
"babel-preset-env": "^1.7.0",
"express": "^4.16.4"
},
"devDependencies": {
"#babel/cli": "^7.0.0-rc.1",
"#babel/core": "^7.0.0-rc.1",
"#babel/node": "^7.0.0-rc.1",
"#babel/preset-env": "^7.0.0-rc.1",
"nodemon": "^1.18.3"
}
Run with babel-node. (I can see it's already installed as dev dependency)
"serve-dev": "nodemon --exec babel-node src/index.js"
(assumed that src/index.js is your entry point of your app)
I think nodemon has built-in support for this now.
Create a config file: https://github.com/remy/nodemon#config-files
Watch for the restart event and put your build script stuff there: https://github.com/remy/nodemon#triggering-events-when-nodemon-state-changes

Nodemon ''npm' is not recognized as an internal or external command

I realize that this is most likely a duplicate question. I'm new to nodemon and I'm trying to establish a server for a Vue JS project with nodemon. I'm trying to run eslint with nodemon and can't figure out why I keep getting the error message. If I remove npm after --exec it will tell me ''run' is not recognized, and if I remove that I will get ''lint' is not recognized and so on.
My package.json file:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run lint && node'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I have also tried this code in my start scripts:
"scripts" : {
"start": "./node_modules/nodemon/bin/nodemon.js src/app.js --exec 'npm run lint && node'",
"lint": "./nodemodules/.bin/eslint **/*.js"
}
Where is tells me that "." is not recognized as an internal external command.
I've installed nodemon into my server folder and in the project directory as well as globally. I have done the same with eslint as well.
i had the same problem today. did some google stuff and found that this is not working anymore. so i tried this
"scripts": {
"prestart": "npm run lint ",
"start": "nodemon src/app.js ",
"lint": "./node_modules/.bin/eslint src/*.js"
},
when you npm start node will run the pre-start script before the start script.Once a file being updated this pre-start wont run by the nodemon.So for that we have to call the nodemon events.So create a nodemon.json on root folder and paste following.
{
"events": {
"restart": "npm run lint"
}
}
you can read more nodemon config options from here nodemon config .There are more nodemon events.you can read them from here event restart
PS:im very new to this. :)
EDIT1:
You can use as follows. this dont need a nodemon config;
"scripts": {
"start": "node src/app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.js --exec \"npm run lint --fix && node\"",
"lint": "eslint --fix **/*.js "
}
for run use npm run dev it will run es lint + nodemon. this is for windows cmd command.if you are using bash terminal, remove \ in "dev" ;
"dev": "nodemon src/app.js --exec "npm run lint --fix && node""
I had the same problem.
For some reason you can't use simple quotes in npm scripts.
Use escaped double quotes instead. This should work:
"start": "nodemon src/app.js --exec \"npm run lint && node\""
install it globally for making it available on path.
npm i -g nodemon
or if using yarn
yarn global add nodemon
and if you tried this approach and it didn't work.
you should try running it locally..
you have to create a script in your package.json like this
"script": {
"server" : "nodemon scriptFile.js" //name of the file you want to run
}
then use,
npm run server
but before it,
install nodemon locally.
check it, if it is available on package.json

How to setup create-react-app with webpack-dev-server

I'm trying to setup my create-react-app instance with webpack-dev-server.
This is my package.json file
{
"name": "reactgs",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-scripts": "1.0.14"
},
"scripts": {
"start": "react-scripts start",
"build": "webpack-dev-server --entry ./node_modules/react-scripts/bin/react-scripts.js --output-filename ./dist/bundle.js --inline --progress",
},
"devDependencies": {
"webpack-dev-server": "^2.9.1"
}
}
I don't have a webpack.config.js file.
When I run
npm run build
The output appears as
The following line runs fine.
npm start
I also have webpack setup fine on the same machine for a different project.
Does anyone know how to properly configure webpack for create-react-app?
npm start does the HMR for you out-of-the box. It will host your application using webpack-dev-server --hot itself. You just need to hook the HMR code to tell webpack what to watch and reload as necessary.
This GitHub conversation offers a pretty decent simple solution for hooking HMR (https://github.com/facebookincubator/create-react-app/issues/2317) with and without Redux.

nodemon is unable to find ts-node in exec-parameter

I'm trying to use ts-node with nodemon. Both are installed using yarn and my package.json has the following structure:
{
"name": "yarnTest",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"#types/express": "^4.0.36",
"bootstrap-sass": "^3.3.7",
"nodemon": "^1.11.0",
"typescript": "^2.4.1"
},
"dependencies": {
"#types/chalk": "^0.4.31",
"chalk": "^2.0.1",
"express": "^4.15.3",
"ts-node": "^3.2.0"
},
"scripts": {
"dev": "nodemon --exec 'ts-node --cache-directory .tscache' ./src/www.ts",
"start": "ts-node --fast ./dist/www.ts"
}
}
Now, when I use "yarn run dev", it executes nodemon and nodemon tries to execute "ts-node" but nodemon tells me that the command "ts-node" does not exist:
Der Befehl "'ts-node" ist entweder falsch geschrieben oder konnte nicht gefunden werden.
Yarn is installed globally but ts-node is installed for my project only.
I already tried:
"scripts": {
"dev": "nodemon --exec 'yarn run ts-node --cache-directory .tscache' ./src/www.ts",
"start": "ts-node --fast ./dist/www.ts"
}
But this gives me the error that "yarn" is not found :(
Any ideas how to fix this issue?
You should install ts-node first, run npm install -g ts-node
I finally solved the issue!
After some hours I figured out that nodemon told me, that it's unable to find "'ts-node" (or "'yarn"). The apostroph was confusing me, so I finally replaced both apostrophes in my package.json with " and now my working script command is the following one:
"dev": "nodemon --exec \"ts-node --cache-directory .tscache\" ./src/www.ts"
2020 Update
I am trying to run a typescript file with nodemon and following dev script in package.json is sufficient
{
"dev": "nodemon src/index.ts"
}
No need of including ts-node in the dev script.
I have ts-node and nodemon in dependencies

Resources