I'm trying to get my first ever node app ready for a production server.
build and serve scripts I'm basing on that given by babel
"scripts": {
"start": "nodemon --exec babel-node server.js --ignore public/",
"build": "babel server.js -o server_compiled.js",
"serve": "node server_compiled.js",
"dev": "webpack -wd"
}
npm run build
works as expected
npm run serve
results in an error:
some/path/config.js:3
export default{
^^^^^^
SyntaxError: Unexpected token export
This is from a config file that I'm referencing in server.js for port host and db.
Do I build this file also or what have I done wrong?
Any help appreciated.
So, I eventually figured out I had to compile all of the .js files in the app individually. I'm sure there must be automated way to do this but thought I'd leave the solution in case anyone can't find anything else.
Related
I saw lot of different ways, some looked normal some others looked a bit more patchworked.
Can we use package json script to chose our env variables ? What is the right way to do it with nodeJS and how to do it ?
I have already made an .env. It contains api keys which are global for dev and prod. But I have some variables, the URL variable for exemple, which won't be the same depending on dev or prod.
Here are my scripts in the package.json
...
"scripts": {
"dev": "nodemon app.js",
"prod": "node app.js"
}
Use cross-env package to define a NODE_ENV for the command you are running. e.g. "prod": "cross-env NODE_ENV=production node app.js"
In the code, read the env file based on the NODE_ENV config. FWIW dotenv package can help with reading .env files.
There`s a more easy way to load your env vars using .env files.
Just add --require dotenv/config to your start script, like: node --require dotenv/config server.js.
https://github.com/motdotla/dotenv
But, the problem is that this does not seems to work with nodemon and I can`t figure out how to do it.
I tried:
"start:dev": "nodemon --require dotenv/config",
Can someone help?
According to this GitHub issue, nodemon does not accept cli parameters for node. However, you can use this workaround to pass params:
nodemon --exec "node -r dotenv/config" index.js
You can put this in your npm start command by editing the package.json:
"start": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts",
^^^^^^^^^^^^^^^
If you want to use it in the start:dev command, edit the nodemon.json file:
"exec": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts"
^^^^^^^^^^^^^^^^
if someone has reached here looking for a generic answer of how to set environment variables in nestjs then you got to read this official docs Nestjs Configuration
I have a Node.js app using TypeScript and now I want Jasmine to run tests automatically each time I make changes in .ts files. So I'm just trying to find an appropriate command to be run as npm test in command line or a package that can watch my .ts files compile them on changes and run jasmine. Does anybody know a solution for it?
The easiest way I found is
installing dependencies: npm install --save-dev jasmine-ts nodemon
initializing jasmine: node_modules/.bin/jasmine-ts init
In the package.json:
"scripts": {
"test": "nodemon --ext ts --exec 'jasmine-ts \"src/**/*.spec.ts\"'"
}
Edit: the above solution doesn't work as of the 11th of Apr, 2019. I published a modified working example at https://github.com/erosb/ts-node-jasmine-example
This may be done with two commands launched in separate terminals. Assuming packages are installed in global mode.
First command launches TypeScript compiler in watch mode:
tsc --watch
The second starts nodemon that watches .js files and restarts on changes. Each time it executes jasmine test runner:
nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'
This solution is fast enough though it also has a drawback of running in two terminals. So it is not ideal but the best I've found so far.
As a result scripts section in package.json looks like:
"scripts": {
/* ... */
"watch": "tsc --watch",
"test": "nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'",
"devstart": "nodemon ./bin/www"
},
devstart also works in couple with watch restarting server each time .ts files are changed (after they are compiled to .js).
You might consider using jasmine-node. I don't think that jasmine itself has a watch option.
npm i -g jasmine-node
Assuming that your test command in your package.json scripts block is something like this:
"scripts": {
...
"test": "jasmine some-directory-or-glob-pattern"
...
}
Use jasmine-node and add the --autotest and --watch flags to that command:
"scripts": {
...
"test": "jasmine-node --autotest --watch some-directory-or-glob-pattern"
...
}
Previously described methods either did not work, or were slow to compile code. Here is my attempt to solve this, both fast and convenient, works great for me. The only downside is that jasmine would not know which tests are affected by TS recompilation and would run all the tests.
yarn add tsc-watch --dev
yarn run tsc-watch --onSuccess "yarn run jasmine --config=jasmine.json"
NPM version:
npm -i tsc-watch
npm run tsc-watch --onSuccess "npm run jasmine --config=jasmine.json"
In my case I needed to correctly map TS paths. The full command looks like this:
yarn run tsc-watch --onSuccess \
"node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine \
--config=jest/jasmine.json --require=dist/jest/setup.js $targetFile"
jasmine.json
{
"spec_dir": "dist/src",
"spec_files": ["**/*.e2e.js", "**/*.unit.js", "**/*.spec.js", "**/*.test.js"],
"env": {
"random": false
}
}
Just an example, please adjust to your needs.
tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to successful compilation and start tests.
I have been using create react app for a while. 'npm start' or 'yarn start' autoreloads works fine by itself but now I have an another problem. Currently I run the app on express server through the build folder, and I use 'npm run build' since express is serving the built files. There are many api calls which requires the app to be ran through this way. Now it become tedious to manually do 'npm run build' every time. Is there a simple way or work around to build automatically just like 'npm start' without eject the app(I know could eject and configure webpack to do that, but i don't want to go down that path)? Thanks
Unfortunately this is something you will have to do yourself. You can use a tool like npm-watch to accomplish what you want though:
Install npm-watch
npm i --save-dev npm-watch
package.json
{
"name": "react-app",
"version": "0.1.0",
"private": false,
"devDependencies": {
"npm-watch": "^0.1.8",
"react-scripts": "0.9.5",
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"watch": "npm-watch"
},
"watch": {
"build": "src/"
}
}
Afterwards, just use npm run watch to start up npm-watch so it can rebuild your assets on changes.
Update:
React-scripts now includes a proxy option that proxies requests to a different host/port. For example, if your backend is running on localhost at port 9000 under the /api route, then you would add this line to your package.json: "proxy": "localhost:9000/api". You could then make requests as you normally would in production. (source: https://create-react-app.dev/docs/proxying-api-requests-in-development)
While this doesn’t really answer your question, you shouldn’t be using npm run build in development. Not only the rebuilds are slow, but it also skips important React warnings for performance and size, so you’ll end up scratching your head more and getting a lot less details in the warnings.
If you just need to do API requests with Express, use the proxy feature which lets you proxy API requests from npm start to your server. There is also a tutorial with a matching repository demonstrating how to do that.
In production, of course, you should use the build produced by npm run build. But you would only need to run it before deployment.
Run your backend on a different port. If your running on express modify the file bin/www
var port = process.env.PORT || 9000
and in your /src folder create a config file where you configure your api host,routes, params etc
//config/index.js
export const Config = {
protocol: 'http',
host: window.location.hostname, //or the environment variable
params: '/api/',
api: {post:'/posts/'}
}
and in your calling component or where ever your calling the api's
import {Config} from '../config'
axios.get(`${Config.protocol}${Config.host}${Config.params}${Config.api.posts}${some id i guess}`)
The easiest way that I found (as of 10/19/21) is to use cra-build-watch.
Works perfectly.
i am also using create react app, this is how i modified my scripts to run project for development(windows), build the production build and run the production build.
"scripts": {
"start": "set PORT=8080 && react-scripts start",
"build": "react-scripts build",
"deploy": "set PORT=8008 && serve -s build"
}
npm start : run project for development(windows)
npm run-script build : build the production build
npm run-script deploy: run the production build
npm install -g serve before run npm run-script deploy.
1> npm install create-react-app -g
2> create-react-app Your_Apps_Name
I am using npm start to start my MEAN stack application, but I would like to use the node-inspector to debug some Mongoose. I know I can start the node inspector with node-inspector, but what can I substitute node --debug app.js with to make npm start work in my case?
This is my MEAN stack directory structure:
HTML views/
Angular.js public/javascript/
Express.js routes/
Node.js app.js
Mongoose js models/, connected in app.js
Mongo db connected in app.js
For more information, this is my related question.
You may want to add a seperate debug script to package.json. That way you won't have to remember to revert npm start when you're finished debugging.
"scripts": {
"start": "node ./bin/www",
"debug": "node --debug ./bin/www"
}
Start with npm run:
$ npm run debug
In package.json modify the start run command:
"scripts": {
"start": "node --debug app.js"
}
I use it like this, I also set a variable and run the inspector in one command:
npm run debug
"scripts": {
"start": "set SOAPAPI=https://example.com/&&nodemon",
"debug": "start node-inspector --web-port=8081&&set SOAPAPI=https://example.com/&&nodemon --debug"
}
*nodemon is an utility wrapper for node, you can use node instead