I have installed the bable-cli and the babel-preset-env, after installing nodemon the npm start fail to load my application when using the following in the package.json file:
"scripts": {
"start": "nodemon src/index.js --exec babel-node"
}
Showing the following error:
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `babel-node src/index.js`
'"node"' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
But everything works fine when using the following:
"scripts": {
"start": "babel-node src/index.js"
}
Thanks!
I ran into the same problem and solved it this way:
"scripts": {
"start": "babel-node src/index.js"
"dev" : "nodemon --exec npm start"
}
The reason is because you are not sending anything to babel to transpire and run. nodemon just looks for file changes.
Try this instead.
"serve":nodemon --exec babel-node src/index.js
If that doesn't work make sure you have the proper DEV dependencies.
#babel/core
#babel/node
#babel/cli
#babel/preset-env
configure .babelrc to have the following
{
"presets" : ["#babel/preset-env"]
}
Related
I got the source code from someone I know. But he uses Linux and I use Windows.
I used the following command to run this program.
"scripts": {
"dev": "nodemon --watch src --watch package.json --watch tsconfig.js --delay 1 --exec 'cross-env HTTP_PORT=4080 NODE_ENV=develop ts-node' src/index.ts",
"lint": "tslint -p .",
"build": "tsc",
"dto:build": "tsc src/dto/*.ts --declaration --emitDeclarationOnly --experimentalDecorators --outDir dto",
"dto:publish": "rm -rf dto; npm run dto:build && cp package.dto.json dto/package.json && cd dto && npm publish --access public"
},
npm run dev
When I run this script, I got the following result.
[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): src\**\* package.json tsconfig.js
[nodemon] watching extensions: ts,json
[nodemon] starting `'cross-env HTTP_PORT=4080 NODE_ENV=develop ts-node' src/index.ts`
''cross-env'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는
배치 파일이 아닙니다.
[nodemon] app crashed - waiting for file changes before starting...
''cross-env'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는
배치 파일이 아닙니다.
<< this mean 'cross-env' is not recognized as an internal or external command (maybe)
As a result I don't know how to solve this.
I also installed cross-env using npm install.
I expect this to be a different command for Linux and Windows.
I would appreciate it if you could tell me the cause of this and how to fix it.
I'll attach a rough file structure.
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.
This is my nodemon.json
{
"watch": ["src/**/*.ts"],
"exec": "node out/index.js"
}
I run the nodemon by executing:
nodemon
In root nodejs directory
This is output:
% nodemon
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*.ts
[nodemon] starting node out/index.js
Yay! Started app!
But when I edit any ts file in src nodemon doesn't restart the app.
UPDATE
Running nodemon --watch src/index.ts --exec 'node out/index.js'
Works and reloads the app on modifying index.ts
However, running with wildcard
nodemon --watch 'src/**/*.ts' --exec 'node out/index.js'
or
nodemon --watch src --exec 'node out/index.js'
Doesn't reload the app.
Solved!
By running nodemon in verbose mode I have discovered that by default it watches only *.js files, regardless of what wildcard you are watching. Therefore adding -e ts to the command fixes the problem:
nodemon --watch src/ --exec 'node out/index.js' -e ts
If someone uses nodemon.json here is mine after fix:
{
"watch": ["src"],
"exec": "tsc && node out/index.js" ,
"ext": "js, json, ts, proto"
}
I didn't have any luck with src/ watching either. I'm watching files via nodemon --watch '**/*' this will finds any changes in the nested files
Use single quotation for multi-value args like `--exec' in the package.json script.
e.g. I changed "nodemon --exec npm run build-langs" to "nodemon --exec 'npm run build-langs'" and worked.