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

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

Related

How to run a Node.Js backend project from another developer?

I'm new to Nodejs, normally when I make a project, I install the dependencies myself, and in order to run the project set something like this in the packaje.json:
"scripts": {
"dev": "nodemon server.js"
}
and then I just run my project with something like this npm run dev
But in a project made by someone else it doesn't seem to be that simple because it has the dependencies like this:
{
"name": "proyecto API",
"version": "1.0.0",
"description": "proyecto Main Backend Repo",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cross-env NODE_ENV=production node --harmony index.js",
"dev": "cross-env NODE_ENV=development nodemon --harmony index.js",
"lint": "eslint --ext .js --ignore-path .gitignore .",
"lint:fix": "eslint --ext .js . --fix",
"superuser": "node --harmony createSuperUser.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/proyecto-Network/lolagato"
},
"author": "gatito",
"license": "MIT",
"bugs": {
"url": "https://github.com/proyecto-Network/lolagato/issues"
},
"homepage": "https://github.com/proyecto-Network/lolagato/erths",
"dependencies": {
"#google-cloud/storage": "^5.14.2",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongo-project": "^1.0.1",
"mongodb": "^4.1.0",
"morgan": "^1.10.0"
},
"devDependencies": {
"cross-env": "^7.0.3",
"nodemon": "^2.0.12"
}
}
Apparently here I can't just do npm run dev because as you can see in the packaje.json it comes out this:
"dev": "cross-env NODE_ENV=development nodemon --harmony index.js"
So my questions are, how can I run this project on my PC and what does that say "cross-env" and --harmony mean?
I have to install all that that appears in the packaje.json?
Just do,
npm install
This will install all the required dependencies. If you notice, in the devDependencies there is cross-env which is required for your npm run dev.
Once the dependencies are brought up, do npm run dev.
Or you could just do -
nodemon index.js
or
nodemon --harmony index.js
To see what happens.
cross-env NODE_ENV=development is just setting the environment variables. From their npm page -
cross-env makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform.

nodemon: not found on heroku

My app runs locally with no issues by its failing on Heroku
package.json
{
"name": "app name",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"engines": {
"node": "14.17.0"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.12.9"
}
}
Procfile
web:node app.js
its returning the below error
sh: 1: nodemon: not found
Any help on how to make this work?
The nodemon is installed globally on your machine that's why you are able to run it on your machine. When you are pushing it on Heroku it is showing the error because there is nothing like nodemon.
Also, Heroku doesn't need nodemon so just replace your start script:
node app.js
and change your Procfile to this:
web: npm start
Even I had the same issue.
You just have to remove nodemon from start script
It has to be iin this way;
"scripts": { "start": "src/index.js", "dev": "env-cmd -f ./config/dev.env nodemon src/index.js" }

How to run Nodemon with TypeScript in debug mode in WebStorm

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

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 start script and running eslint

I'm starting a project in Vue.JS and I'm a little new to nodemon.
Here is my package.json file
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run eslint'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I can get nodemon to run through app.js with "nodemon src/app.js". I've tried a whole bunch of combinations after --exec and haven't had any luck.
The correct way is (in package.json and windows):
"scripts": {
"start": "node index",
"start-dev": "nodemon --exec \"npm run lint && node index\"",
},
This works pretty fine for your use-case.
nodemon src/app.js --exec "npm run lint && node"
or you can write nodemon.json file in your root directory
{
"watch": ["src"],
"ignore": ["**/*.test.js", "**/*.spec.js", ".git", "node_modules"],
"exec": "npm run lint && node src/app.js"
}
Im using nodemon version 1.19.4. You just missed the "events" key. The right way would be to create a nodemon.json in your root folder like that, then a lint script in your package.json with your lint command:
{
"watch": [ "src" ],
"ignore": ["**/*.test.js", "**/*.spec.js", ".git", "node_modules"],
"events": {
"restart": "npm run lint"
}
}
Here you can check about Nodemon Events.
When using events you don't need to manually handle your application state (restart, crash, node execution, etc), just put what you want to happen when nodemon refreshes.
I've been using a custom script for a while now which I finally published to npm.
Check it out here: https://github.com/theoephraim/lint-fix-nodemon
This helps avoid double restarts when eslint fixes your files as well as not failing on the initial run if eslint has fatal errors.
Hope it helps!

Resources