I'd like to get my package.json to be able to run using the command npm run test-watch on Windows 10 with npm 5.5.1. In my package.json:
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
}
However, I this interpretes the code strangely to have a single quote in there. I'm actually following a Udemy course so it appears to work for the instructor. However, here is the output I get:
PS D:\courses\node-course\node-todo-api> npm run test-watch
> todo-api#1.0.0 test-watch D:\courses\node-course\node-todo-api
> nodemon --exec 'npm test'
[nodemon] 1.14.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `'npm test'`
''npm' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
What do I need to change to get this to work? It appears to be keeping the quotes on the string. I can't seem to get around it though. When I run the command directly, it works:
PS D:\courses\node-course\node-todo-api> nodemon --exec 'npm test'
[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm test`
> todo-api#1.0.0 test D:\courses\node-course\node-todo-api
> mocha server/**/*.test.js
started on port 3000
Post /todos
√ should create a new todo (50ms)
1 passing (1s)
Unfortunately, the operating system and shell can cause a massive headache when using npm. Some things work on one computer and some on another.
Both of these should work on Windows 10 though:
"test-watch": "nodemon --exec \"npm test\""
"test-watch": "nodemon --exec npm test"
Related
I have a simple node server written in typescript. My package.json is configured as:
"scripts": {
"build": "tsc",
"dev": "nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts",
"debug": "nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts"
},
When I run npm run dev nodemon will launch the server and restart it when any changes are made.
[02/28/18 20:45:53] npm run dev
> pq-api#1.0.0 dev C:\Users\joe\pq\pq-api
> nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*
[nodemon] starting `ts-node ./src/server.ts`
initializing config to development
info: PQ-API running on port 3000
However, when I run npm run debug (so I can attach a debugger) It looks like it begins to start, but just hangs forever
[02/28/18 20:39:30] npm run debug
> pq-api#1.0.0 debug C:\Users\joe\pq\pq-api
> nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 10156 to restart
[nodemon] watching: src/**/*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node --inspect ./src/server.ts`
[nodemon] spawning
[nodemon] child pid: 13344
[nodemon] watching 12 files
That is all the output has. The script is never executed; the server never starts up, and the inspector is never available to connect to.
node 8.94
nodemon 1.15.1
ts-node 5.0.0
typescript 2.7.2
With ts-node 5.0.0 you no longer pass the --inspect flag the same way. The suggested way is node --inspect -r ts-node/register path/to/ts. For example:
nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register src/app.ts
see https://github.com/TypeStrong/ts-node/issues/537
Provide location and port to the inspect option like:
--inspect=0.0.0.0:9200
I am using PHP Storm and the previous answer of #user60456 works like a charm for me.
With some changes, I was able to run debug mode using dotenv with multiple env files in PHP Storm as well.
package.json
"start:dev": "nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register -r dotenv/config local.ts dotenv_config_path=./.env.development",
where local.ts is the file where my app.listen() is.
Then, I had to create a new Run configuration (Attach to Node.js/Chrome) with:
host: localhost
port: 9229
Attach to: Chrome or Node.js > 6.3 started with --inspect
Then select the root folder in Remote URLs of local files tab and set the Remote URL to /usr/src/app.
Now, you can run npm run start:dev. You will see the console output:
Debugger listening on ws://127.0.0.1:9229/...
You have to run the Nodej.js debug configuration now and wait, until you see the console output:
Debugger attached.
The application is now running in debug mode.
I made changes using some of the info above as mine just didn't seem to work and the change I made did resolve the problem.
From "start": "tsnd --inspect -- src/app.local.ts"
To: "start": "node --inspect -r ts-node/register src/app.local.ts"
I just fixed this problem by writing nodemon.json file like this :
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"execMap": { // [A]
"ts": "node --require ts-node/register"
},
"watch": ["src/"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json,ts"
}
ref : https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7
I had a different issue resulting in the debugger never running. I was running this command,
node index.js --inspect=5005
instead of this,
node --inspect=5005 index.js
The flag is supposed to be before the source file.
In my package.json file I write this run nodemon:
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
},
And this error showed in terminal:
> node-todo-api#1.0.0 test-watch D:\nodepractice\node-todo-api
> nodemon --exec 'npm test'
[nodemon] 1.12.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `'npm test'`
''npm' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
How to resolve this problem, I want to use nodemon.
Instead of putting in single quotes why don't you put in double quotes by escaping them.
Try this:
"test-watch":"nodemon --exec \"npm test\""
If you are working on Windows try to remove the single quotes after the exec command:
"test-watch": "nodemon --exec npm test"
I have a project written in TypeScript and running on Node. I'm really struggling to write the script with npm to get it running for development.
What I trying to do is:
clean the /dist folder
if a .ts change, compile it into /dist and restart node
Here is my 1st attempt, from the scripts section of my package.json:
"clean": "rimraf dist/**/*",
"build": "tsc",
"watch:start": "npm run clean && nodemon -e ts --exec \"npm run start\"",
"start": "npm run build && node dist/index.js"
If I start my project with npm run watch:start, it got stuck in a loop:
npm run watch:start
> nodemon -e ts --exec "npm run start"
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm run start`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `npm run start`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
Here is my 2nd attempt, using npm-run-all to run several task in parallel:
"clean": "rimraf dist/**/*",
"build": "tsc",
"watch:start": "npm-run-all clean build --parallel --race watch:build watch:serve --print-label",
"watch:build": "tsc -w",
"watch:serve": "nodemon dist/index.js"
This one works better but it still restart node several time at startup.
Suggestions and improvements welcome !
You can use tsc-watch, which omits the use of nodemon and runs when any changes affect the typescript source of your app.
"scripts": {
"dev": "tsc-watch --onSuccess \"npm start\" ",
"start": "node index.js"
}
It has a success handler --onSuccess which restarts the server everytime a change is made to the typescript source.
npm run dev
index.js
console.log('node run')
setTimeout(() => console.log(Math.random() * 1000.0), 1000);
console
npm run dev
> tsc && concurrently "npm run node-tsc:w"
npm WARN invalid config loglevel="notice"
> jsperf.com#0.1.0 node-tsc:w C:\Users\Shane\Desktop\so
> tsc-watch --onSuccess "node index.js"
16:49:31 - Compilation complete. Watching for file changes.
node run
709.2226373633507
16:49:36 - File change detected. Starting incremental compilation...
16:49:37 - Compilation complete. Watching for file changes.
node run
974.6349162351444
16:49:41 - File change detected. Starting incremental compilation...
16:49:41 - Compilation complete. Watching for file changes.
node run
935.9043001938232
Node is restarted after changing the typecript source.
I am new to node development on power system i, was trying to get nodemon to work but errors out with following:
# nodemon.js
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
[nodemon] Internal watch failed: watch ENOSYS
My application uses express, ejs and body-parser, works well outside of nodemon. First off, I couldn't get nodemon to be installed globally so I put that on my application node_modules directory and set the path, which is not what I usually do in other environments. How does npm install -g suppose to work on system i?
Any help on this greatly appreciated!
Thanks!
My environment is:
(node: v6.9.1)
(npm:3.10.8)
(OS: V7R1)
Path: /QOpenSys/QIBM/ProdData/OPS/Node6/bin:/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin:/home/QSECOFR/projects/mytasklist/node_modules/nodemon/bin
LIBPath: /QOpenSys/QIBM/ProdData/OPS/Node6/bin:/home/QSECOFR/projects/mytasklist/node_modules/nodemon/bin
After days of searching, I think I found a solution.
I wasn't able to run directly Nodemon by typing
nodemon src/app.js
So on Node14 I found a workaround, adding a script line in package.json
... ,
"main": "./src/app.js",
"scripts": {
...,
"serve": "nodemon -L"
},
Then run your application by typing
npm run serve
And voilà !
It seems to run in legacy mode.
Note : In node 10 this worked too (but not in node 14) even if my main file is src/app.js
"serve": "nodemon server.js"
I have installed nodemon using command:
npm install nodemon -g
Having done that I changed "start": "node ./bin/www" to "start": "nodemon ./bin/www"
Output in Console on running npm start :
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `nodemon ./bin/www ./bin/www`
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `nodemon ./bin/www ./bin/www ./bin/www`
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `nodemon ./bin/www ./bin/www ./bin/www ./bin/www`
and so on....
Where am I going wrong? Please bear in mind that this is my 3rd day on nodejs so keep it simple.
Edit
"scripts": {
"start": "nodemon app.js"
},
"nodemon app.js" seems to work since the console does not show any errors but then I am unable to run the application. However if I change it back to node ./bin/www it would work.
Try to downgrade requirements.
This helped me.
npm install nodemon#1.0.0 -g
I reproduced this issue in docker image (node:alpine) with nodemon >1.2.0 100%. And v1.1.0 and v1.2.0 have some strange issues too.
I checked only minor releases (w/o checking patchlevels e.g. 1.2.1, 1.2.2 and others)
So. 1.0.0 is old, but works well.
On my host Mac I have nodemon#1.11.0 installed globally.
Sometimes I have this issue and sometimes I don't.
This is somehow connected with pwd and nodemon.json file. But I'm not sure.
I found a bug report related to this problem.
You are watching a file that doesn't exist and this will cause an infinite loop.
In your case, you are running ./bin/www and looking at you IDE screenshot you have a ./bin/www.js
That's why nodemon app.js doesn't error. Although I can't guaranty it's going to work try nodemon ./bin/www.js
Then, without looking at the code, I'm not sure why you can't connect your application that way.
For more info check #matt answer and nodemon docs.
You don't need to tell nodemon what folder to watch. You only need to tell it to run your main application and it will automatically watch all the nested folders and files.
eg. nodemon app.js (if app.js is your application)
Also. You can optionally create a nodemon.json file in the main body of your application to house config information for nodemon. Not really related to your queestion. But good to have :)
Here is what mine looks like:
{
"ignore": ["data/*.json", "/node_modules/", "README.md"]
}
Try with this :
npm i -D nodemon
in package.json add:
"scripts": {
"start": "nodemon index.js",
}
This executes the file index.js that is in the root of your app node.
For example:
app/
node_modules
index.js
try again:
npm start