How does express know to run "server.js"? - node.js

I cracked open an experimental express project I hadn't touched for a couple of months, and I tried to trace through how everything works to refresh my understanding.
What confuses is me is that my express app is run from a file called server.js, but that is not specified anywhere in my package.json, nor is there any reference to such a filename (like as a default) in the node_modules folder for express. It works though, unless I rename the file (e.g. server_.js). I have no scripts setup in package.json. Whatever I specify for "main" in package.json seems to have no effect.
So how does npm start know to run the app from server.js?
{
"name": "my thing",
"version": "0.0.1",
"description": "This is my thing.",
"main": "index.js",
"scripts": {
"test": "test"
},
"repository": {
"type": "git",
"url": "my-thing"
},
"author": "Faust",
"license": "ISC",
"dependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"material-ui": "^1.0.0-beta.4",
"material-ui-icons": "^1.0.0-alpha.19",
"express": "^4.15.4",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.5",
"react-router": "^4.1.2",
"react-router-dom": "^4.1.2",
"redux": "^3.7.2",
"webpack": "^3.3.0"
}
}
ls of the root:
data/ entry.jsx node_modules/ npm-debug.log package.json public/ server.js src/ webpack.config.js

From the official NPM docs -
npm start runs an arbitrary command specified in the package's "start" property of its "scripts" object. If no "start" property is specified on the "scripts" object, it will run node server.js.
Read more here.

Related

When I try to use Parcel to build a production version of a React app that works fine in development mode, I get "Cannot find module 'sass'"

I've built a simple React/Redux app and am using Parcel for the bundler. It works fine in development mode, that is, using the script
"dev": "parcel ./src/index.html",
but when I tried to do
"build": "parcel build ./src/index.html",
I got the following error
/Users/abc/Documents/Projects/sandbox/smart/src/styles/main.scss: Cannot find module 'sass' from '/Users/abc/Documents/Projects/sandbox/smart/src/styles'
at /Users/abc/Documents/Projects/sandbox/smart/node_modules/resolve/lib/async.js:97:35
at processDirs (/Users/abc/Documents/Projects/sandbox/smart/node_modules/resolve/lib/async.js:244:39)
at isdir (/Users/abc/Documents/Projects/sandbox/smart/node_modules/resolve/lib/async.js:251:32)
at /Users/abc/Documents/Projects/sandbox/smart/node_modules/resolve/lib/async.js:23:69
at FSReqWrap.oncomplete (fs.js:152:21)
This is the first time I've tried to build an app using Parcel and I'm lost. I'm using node-sass to import my sass files directly -- perhaps this has something to do with that.
The referenced file (main.scss) is referenced in App.js like this
import "../../styles/main.scss";
and is the first sass file to be included (App is loaded into the DOM like this in index.js:
const store = configureStore()
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
)
This is running under Node 11.15. My package.json follows -- any hints or help much appreciated!
{
"name": "friender",
"version": "1.0.0",
"description": "",
"main": "index.js",
"jest": {
"setupFiles": ["jest-localstorage-mock"]
},
"scripts": {
"dev": "parcel ./src/index.html",
"build": "parcel build ./src/index.html",
"lint": "eslint \"src/**/*.{js,jsx}\" --quiet",
"test": "jest"
},
"prettier": {
"semi": false
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"#babel/cli": "^7.6.0",
"#babel/core": "^7.6.0",
"#babel/plugin-transform-runtime": "^7.5.5",
"#babel/preset-env": "^7.6.0",
"#types/jest": "^24.0.18",
"babel-eslint": "^10.0.3",
"babel-preset-react": "^7.0.0-beta.3",
"eslint": "^6.2.2",
"eslint-config-prettier": "^6.1.0",
"jest": "^24.9.0",
"jest-localstorage-mock": "^2.4.0",
"parcel-bundler": "^1.12.3",
"prettier": "^1.18.2"
},
"dependencies": {
"#emotion/babel-preset-css-prop": "^10.0.14",
"#emotion/core": "^10.0.16",
"#emotion/styled": "^10.0.15",
"#fortawesome/fontawesome-svg-core": "^1.2.0-7",
"#fortawesome/free-solid-svg-icons": "^5.11.1",
"#fortawesome/react-fontawesome": "^0.1.4",
"axios": "^0.19.0",
"bulma": "^0.7.5",
"eslint-plugin-jest": "^22.17.0",
"eslint-plugin-react": "^7.14.3",
"immer": "^4.0.0",
"node-sass": "^4.12.0",
"prop-types": "^15.7.2",
"react": "16.x",
"react-detect-offline": "^2.4.0",
"react-dom": "^16.9.0",
"react-fontawesome": "^1.6.1",
"react-modal": "^3.10.1",
"react-redux": "^7.1.1",
"redux": "^4.0.4",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0"
}
}
Try instead per the documentation for SCSS and error Cannot find module 'sass' use module sass instead of node-sass:
npm install -D sass
The documentation mentions being able to use node-sass, but there are several active issues with node-sass. Module sass may help resolve the error and allow you use SCSS in your application.
Hopefully that helps!

Travis CI showing build error for my expressjs app despite showing all tests passed

I have integrated Travis CI into my github repo called Banka which contains expressjs application and some html and css files.
On building, travis shows all my test which I wrote in mocha and chai as passing but yet it finally reports "build error" and surprisingly enough, can't point where the error is.
I changed to latest node and npm versions and still, no change
--package.json
{
"name": "Banka",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www",
"test": "mocha"
},
"engines": {
"node": "~10.15.3",
"npm": "~6.4.1"
},
"dependencies": {
"bcrypt": "^3.0.5",
"body-parser": "~1.8.1",
"chai": "^4.2.0",
"chai-http": "^4.2.1",
"cookie-parser": "~1.3.3",
"debug": "~2.0.0",
"dotenv": "^1.2.0",
"express": "~4.9.0",
"express-jwt": "^3.0.1",
"jade": "~1.6.0",
"jsonwebtoken": "^5.0.2",
"mocha": "^6.1.2",
"morgan": "~1.3.0",
"passport": "^0.2.2",
"passport-local": "^1.0.0",
"request": "~2.51.0"
}
}
Here is what Travis is showing : https://travis-ci.com/NawasNaziru/Banka/builds/108177363
I expect to see "build passing" since, all my tests passed.
The cause of the problem is that mocha doesn't close and handover to Travis after running the written tests. Hence, the reason why, travis reports timeout. To fix that, simply add the --exit flag in your package.json next to mocha like this
{
"test" : "mocha --exit"
......
}

How do I overcome build-time errors due to relative paths and custom imports with Next.JS applications?

This question has been answered in response in the context of Python, PHP, etc., but I cannot find an answer specific to this Next.JS blog I am creating. Each time I build, I get the following error from nodemon:
These dependencies were not found:
* /api/posts in ./pages/index.js
* /components/Post in ./pages/index.js
* /layouts/Main in ./pages/index.js
* /routes in ./pages/index.js
As you can see from the screen grab of my project folder, all the custom dependencies thread from my project folder.
My package.json file looks like this:
{
"name": "revised-tottm",
"version": "1.0.0",
"description": "revised TOTTM website",
"main": "_document.js",
"dependencies": {
"next": "^7.0.2",
"next-routes": "^1.4.2",
"nodemailer": "^4.7.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"reactdom": "^2.0.0",
"static-server": "^2.2.1",
"styled-components": "^4.1.2",
"absolute-imports": "^1.0.1",
"body-parser": "^1.18.3",
"express": "^4.16.2",
"get-form-data": "^2.0.0"
},
"devDependencies": {
"babel-preset-env": "^1.5.2",
"babel-preset-react": "6.24.1",
"gulp": "^3.9.1",
"gulp-babel": "^8.0.0",
"gulp-concat": "^2.6.1",
"gulp-imagemin": "^5.0.3",
"gulp-livereload": "^4.0.1",
"gulp-uglify": "^3.0.1",
"gulp-uglify-es": "^1.0.4",
"imagemin-jpeg-recompress": "^5.1.0",
"imagemin-pngquant": "^6.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "next",
"build": "next build",
"start": "next start"
},
"author": "Joel J. Warne",
"license": "ISC"
}
And I have tried variants, e.g. "require('./api/posts')" and "require('api/posts/')" but nothing seems to find them. For instance, if I change the paths to "require('./api/posts/')" and use a Next.JS "build" script, I get the following error:
> Failed to build
{ Error: (client) ./pages/index.js
Module not found: Error: Can't resolve './api/posts' in '/Users/USERNAME/Library/Mobile Documents/com~apple~CloudDocs/WebDevStudio/Revised TOTTM/pages'
# ./pages/index.js 10:0-39 37:19-27
# multi ./pages/index.js
at /Users/USERNAME/Library/Mobile Documents/com~apple~CloudDocs/WebDevStudio/Revised TOTTM/node_modules/next/dist/build/index.js:144:31
The entry point is correct, the project folder structure is correct, etc., but there is no reason that it should not be able to locate, for instance, '/api/posts/' that I can see.
I'm curios if you are importing the modules propertly
For example:
If you want import components/Post into pages/index.js this should looks like this: import Post from '../components/Post';
Let me know if this works,
Regards

Why does NodeJS require me to use the full directory for downloaded modules?

For something like Express for example, which does not come with Node by default. I have to use var express = require('C:/Users/User/node_modules/express'); instead of just var express = require('express');. I notice the modules which come by default such as http aren't in the same location as the ones I install. So what do I need to do in order to not have to write the whole directory. If it makes any difference I keep all my Node projects in C:/Node/, not the default one.
This is happening because you probably don't have node modules installed locally. For that you need a package.json file which you can get by running
npm init
This will ask you some questions about your project and will set up node locally. A package.json file will be created which should look something like this (without dependencies).
{
"name": "express-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "Link to your repository"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^1.0.2",
"body-parser": "^1.17.2",
"chalk": "^2.0.1",
"compression": "^1.7.0",
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"express": "^4.15.3",
"glob": "^7.1.2",
"moment": "^2.18.1",
"mongoose": "^4.11.3",
"morgan": "^1.8.2",
"passport": "^0.3.2",
"path": "^0.12.7",
"yargs": "^8.0.2"
}
}
You can then add the node modules you want by putting them in dependencies and running
npm install
If you want to add node modules from commond line you can use
npm install package-name --save

ember/npm addon with dependencies/ devDependencies

I have created an ember addon which relies on gulp derived packages to build, so in the addon it has the following in package.json.
When the addon is included in the main application, the build fails due to the various gulp modules being missing. The addon is in the devDependencies section of the main application.
The addon also builds into the main application when it is in through npm link. My question is how should the addon dependencies be handled so the main application builds without a local copy of the module?
Update: I think the problem is that the package does a gulp build after the postinstall which elevates the gulp devDependencies into real dependencies. Is this correct?
Addon JSON file
{
"name": "My Addon",
"version": "0.0.0",
"description": "The default blueprint for ember-cli addons.",
"directories": {
"doc": "doc",
"test": "tests"
},
"scripts": {
"build": "ember build",
"start": "ember server",
"test": "ember try:each",
"postinstall": "gulp build && bower install"
},
"repository": {
"type": "git",
"url": "git://github.com/myaddon.git"
},
"engines": {
"node": ">= 0.12.0"
},
"author": "",
"license": "MIT",
"devDependencies": {
"bootstrap": "^4.0.0-alpha.5",
"broccoli-asset-rev": "^2.4.5",
"broccoli-funnel": "^1.0.9",
"ember-ajax": "^2.4.1",
// more ember modules
"find-root": "^0.1.1",
"font-awesome": "^4.7.0",
"glob": "^4.5.3",
"gulp": "^3.9.1",
"gulp-clean-css": "^2.2.1",
"gulp-concat": "^2.6.0",
"gulp-connect": "^2.2.0",
"gulp-filter": "^3.0.1",
"gulp-git": "^1.4.0",
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.3.2",
"gulp-sourcemaps": "^1.5.2",
"gulp-task-loader": "^1.2.1",
"gulp-template": "^3.0.0",
"gulp-uglify": "^1.2.0",
"gulp-zip": "^3.0.2",
"lazypipe": "^1.0.1",
"loader.js": "^4.0.10",
"run-sequence": "^1.1.2"
},
"keywords": [
"ember-addon"
],
"dependencies": {
"ember-cli-babel": "^5.1.7"
},
"ember-addon": {
"configPath": "tests/dummy/config"
}
}
The addon's dependencies are needed to build application (that includes addon), should be listed in dependencies section. The devDependencies of a package that is included as dependency of another package, are ignored.
There is other solution to add packages into an application. You can create a blueprint in your addon that will be called every time an application build with it or install it.

Resources