I'm new to node.js, and I can't seem to run my node app.js file after adding in a new file called projects.js with only this JS code:
exports.viewProject = function(req, res){
res.render("project");
};
This is a pre-coded project from my (useless) professor, and I'm getting this error:
I have Big Sur, uninstalled and reinstalled node, tried sudo, and it still doesn't work. Here is my app.js code as well, and I only added var project and app.get('/project'... etc).
Sorry if this is lengthy, I'm just trying to make it through online classes during a pandemic. Thanks!
The warning that Express is giving you tells you that you're trying to use built-in middleware that aren't included in Express anymore. Some of them have been deprecated over 9 years ago when Express 2.x was deprecated. The current version as of writing is 4, and version 5 is in alpha.
Some of these have not been bundled with Express since 2012 (2.x), some of them since 2015 (3.x):
express.favicon() - handled by express.static() as long as your favicon.ico is inside the public directory
express.logger() - use the morgan package instead
express.methodOverride() - use the method-override package instead, although you probably don't need this
express.cookieParser() - use the cookie-parser package instead
express.session() - probably became express.cookieSession() in 3.x, removed after 3.x, use the express-session package or alternatively cookie-session
As Sebastián mentioned in the comments, app.use(app.router) is also redundant.
Additionally, the express3-handlebars package is also deprecated as it's for Express 3 (like the name suggests). If you go to its NPM page, you'll see a big banner telling you to use express-handlebars instead.
Your package.json file also includes a package named node which, according to its description, can be used to run scripts from dependencies with a specific node version. You also most likely don't need this at all, and can uninstall with npm.
As a closing statement, if the base for this project was provided to you by your educational facility, I would suggest giving them feedback and telling them that it would be time to update the examples.
Related
I made a test project, only trying to include https. I used expo init test to initialize the project, then npm install https to install the library.
I then edited App.js and included this line at the very top: import {https} from 'https';
Finally expo start to start the expo server.
Now I get the following error:
InternalError Metro has encountered an error: While trying to resolve module `https` from file `/path/test/App.js`, the package `/path/test/node_modules/https/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/path/test/node_modules/https/index.js`. Indeed, none of these files exist:
* /path/test/node_modules/https/index.js(.native|.android.expo.ts|.native.expo.ts|.expo.ts|.android.expo.tsx|.native.expo.tsx|.expo.tsx|.android.expo.js|.native.expo.js|.expo.js|.android.expo.jsx|.native.expo.jsx|.expo.jsx|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.wasm|.native.wasm|.wasm)
* /path/test/node_modules/https/index.js/index(.native|.android.expo.ts|.native.expo.ts|.expo.ts|.android.expo.tsx|.native.expo.tsx|.expo.tsx|.android.expo.js|.native.expo.js|.expo.js|.android.expo.jsx|.native.expo.jsx|.expo.jsx|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.wasm|.native.wasm|.wasm)
I've verified that these files do indeed not exist, here is the output of ls node_modules/https: package.json
So I'm guessing I did something wrong during the install process, however, I can't figure out what. (I've also tried several times to install using yarn add https, I got the same error)
The https module you want is almost certainly the Node.js built-in https module and is definitely not the six-year-old https npm module with no documentation or code and merely a lone package.json file. That package should probably be deprecated so people don't make this entirely understandable mistake.
In short, try npm uninstall https and see if your code starts working. It probably will.
If not and you really need some external module, it's definitely not that module. Look at one of the many many great http/https modules out there. #Nitish suggests axios in their answer and that is a popular and solid choice.
import {Agent} from 'https'; should work with the built-in https module assuming you're working with a reasonably recent version of Node.js. (You can quickly test your version of Node.js for this by putting that line of code in an index.mjs file and running node index.mjs. If there's no error message, it worked.)
Disclaimer: I know nothing about expo. This is all from a Node.js/npm perspective.
the error is clear that your package.json don't have https module.
I don't know why you need this module but if you need this for REST API I suggest fetch or if third party you can use axios.
I'm trying to use MQTT npm package as part of a NativeScript application I'm building.
I'm running into a problem when I try to require it.
My code uses the var mqtt = require('mqtt'); as the example indicates, but when trying to compile the NativeScript application, I get the following error :
com.tns.NativeScriptException: Failed to find module: "mqtt", relative
to :/app/tns_modules.
I've verified that the mqtt folder is in my node_modules folder, and I tried creating a tns_modules folder and copying the mqtt folder there as well.
However I'm still getting the error. I tried loading the knock-knock-jokes package and that loads perfectly.
I'm using the latest versions of Node and NativeScript as 13 April 2016 (4.4.0 for Node and 1.7.1 for NativeScript).
Ok, I tried this out to see why this is happening.
The very first line of mqtt.js is
#!/bin/node...
Which is invalid JavaScript code. Node has been programmed to ignore it; but NativeScript does not. (Might be worth a enhancement request..)
So when it loads this file to parse it, it fails to parse and returns that it isn't available. (Not always the best error on parse issues)
HOWEVER, if you fix that error you will run into MORE issues. It will then complain about not finding the "net" library. NativeScript does not have a net library built in. To my knowledge no one has yet created a net replacement for NativeScript. So, in a lot of cases you can use node modules as is; but if they call anything that depends on a built in node library; they unfortunately will not currently work in NativeScript as their is no equivalent library existing.
Just looking at the dependencies of the mqtt library; your odds of getting this working on NativeScript looks at a glance pretty slim.
So I was starting researching about angular2 and since I saw so many references to typescript being prefered I am trying to switch to it from javascript. Problem is, I saw a nice little guide I could follow to install it all in eclipse (angular, javascript, everything needed to launch the 5 minute guide code on the main page of angular), so I did it and managed to get it to work.
Now I would like to do the same with typescript, but I find myself lost since it doesn't seem to be working, I can't launch code, it's like node.js isn't working anymore, but since I do not know what the expected result is, I am not sure what is missing.
For example, I re did the 5 minute guide and notice as a difference that I do not have the installation of the modules, nor any run-as configuration ready, in fact, I can't manage to run as any typescript code I try to do.
What I did was:
1- Install Eclipse Mars, other guys uses older versions
2- Install Node.js
3- Install WildFly Server
4- Install TypeScript plugin https://marketplace.eclipse.org/content/typescript
5- Import my project in eclipse, you may have to create a new static web project and add your files in there. The 5 minute quick start is not an eclipse web project so you may not be able to import this. Do not forget to copy and past the same structure with the node modules that you have
6- Right click on the project > Configure > Enable Typescript Builder
7- Right Click the project > Properties > TypeScript > Compiler and configure as follows:
I hope this will do it for you.
Remember, this plugin does not respect json configuration file, so you have to do this manually as in the screenshot. Also, if you are going to provide arguments to your component constructor, You will have errors. Let me know if you got those. Another thing to mention is that using some annotations like #Input will not work, you will have to use inputs:[] inside your #Component annotation.
You can try to add those plugins on top of Mars:
https://github.com/angelozerr/angular2-eclipse
https://github.com/angelozerr/typescript.java
Those 2 are incubating but already provide good features. There is another one, that I didn't try but which has some popularity:
https://github.com/palantir/eclipse-typescript
Eclipse >= Neon
sudo npm install -g angular-cli (ng help must work)
See: https://github.com/angelozerr/angular2-eclipse
Install it and restart Eclipse
Open a .ts file
Eclipse Oxygen
Default javascript installation, then added the default free plugin for angular 2 / typescript from here:
angular2.ide - http://oss.opensagres.fr/angular2-eclipse/1.3.0/
After installing I opened an existing Angular 4 / TS project, worked fine. I then installed support for .scss from http://www.liclipse.com/text/updates. All setup and configured with Typescript 2.4.1 / nodejs v6.9.4.
But within days it now fails to open .ts files at all. Re-installation of the ng plugin did not fix it, I uninstalled liclipse plugin and no change either. It basically leaves eclipse unusable for ng4 projects as is.
The logs show the following errors.
org.eclipse.core.runtime.CoreException: Plug-in "ts.eclipse.ide.jsdt.ui" was unable to instantiate class "ts.eclipse.ide.jsdt.internal.ui.editor.TypeScriptEditor".
at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.throwException(RegistryStrategyOSGI.java:194)
at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:188)
at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:905)
...
Caused by: java.lang.NoClassDefFoundError: org/eclipse/wst/jsdt/ui/text/JavaScriptSourceViewerConfiguration
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
...
Caused by: java.lang.ClassNotFoundException: An error occurred while automatically activating bundle org.eclipse.wst.jsdt.ui (441).
at org.eclipse.osgi.internal.hooks.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:112)
at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findLocalClass(ClasspathManager.java:529)
...
Caused by: java.lang.ClassFormatError: Name index 1 in LocalVariableTable has bad constant type in class file org/eclipse/wst/jsdt/internal/core/JavaModelManager
I found
TypeEcs plugin for Typescript
it provide below.
•Syntax highlighting
•Code Completion
•Code Outline
•Find References
•Rename / Refactor
•Open Type
•Code Compilation
•Format Code
•Comment Code
•Open Declaration
•Mark Occurences
•Type Script Debug
I already installed Express for a hello world app, and worked nice.Now I want create a new app, How can I use the already installed Express for that new app instead reinstalling it for that new app with : npm install express
Or do I have to re-install it frome internet everytime I create a new app?
npm install express
...will install Express only into the current folder path that you have in your terminal. If you want to install the package for all Node.js instances, you'll need to run:
npm -g install express
or, depending on your server's security model,
sudo npm -g install express
Sometimes you'll need to link the package if the linking failed (you'll get a "Cannot find module X" error), via:
sudo npm link express
If you want to read more about it, this blog post is a good read.
Use npm install -g express
But it worths adding express to your package.json with the rest of (future) dependencies you will need, so you can type npm install in your project's root and it will automatically install all the dependecies with the specified version and so on.
Don't install Express globally
Locally installing any package that you're going to depend on is generally considered a best practice in the Nodejs community. It comes down to managing dependencies.
See: Nodejs Blog - NPM 1.0: Global vs Local Installation
Consider the following scenario:
Lets say you do a global install of Express for your first project. You're start off using the latest version of the library and everything goes well. Then over time you write 10 more applications that depend on that install. Eventually Express hits the next main version and adds some killer features but they've also introduced a few backwards-incompatible API changes. You'll want to use the latest version for a new project but a global update will probably break all of the previous applications you've created.
In a best case scenario, you'll have 100% test coverage on all your old projects and through hard work and determination you will eventually manage to update/fix everything that broke with the update.
Realistically, nobody has 100% test coverage on everything and it's likely that something the update broke will be missed and accidentally pushed into production. ::cringe::
The scenario I just outlined is what lots of people refer to as dependency hell. It's a common reason why some organizations get locked into a specific version of a framework/application/dll.
With nodejs, it's cheap and easy to handle dependencies individually for each project. Modules generally aren't as monolithic (read huge) like the frameworks you'd expect in other languages.
To install Express locally with dependencies just use:
npm install express --save
Note: The --save flag will automatically add Express and the version number to your package.json file. If you want the module marked under the devDependencies listing instead, use the --save-dev flag.
The exception to the rule:
The exception to the don't install locally rule is CLI applications. It's rare that someone will write code that depends on a CLI application and -- even if they do -- CLI apps only superficially expose the highest-order functions. Unless the CLI has a development API that you're project depends on, it's probably safe and more convenient to install the package globally.
Aside: A library developer's perspective
As libraries are updated and improved it's not uncommon for library devs to change the API between major versions (ex 1.0, 2.0, 3.0) as they get a better feel for how the everything should be structured. When backwards-incompatible changes are introduced it's not uncommon for people to get all finger-pointy and start bickering about 'poor design'. Most of those issues have little to do with the design of the libraries being used. Rather, they're a cause of poor design and version management from the devs that implement them.
The truth of the matter is, it's impossible foresee a best possible design for a library until most of the code has already been implemented and put to use by a larger community. The best projects are those that grow organically, have a large userbase providing lots of valuable feedback, and adapt over time to their user's needs.
Major versions are usually the most exciting time for library devs because that's where we actually get to release ground-breaking changes. All the versions in between only serve to exist for boring maintenance and bugfixes.
One of the greatest benefits of Nodejs is it's small core. The Javascript language itself is well defined so there's little/no chance that updates to the core will break any code. The second greatest benefit of Nodejs is that the package manager along with the common package.json project file format make managing dependency versions as easy and straightforward as possible.
Source: I develop libraries and spend an obscene amount of time thinking about good API design.
I've noticed that in trying to get seemingly simple node packages to install with npm (e.g. nerve, a "micro-framework") I often run into some form of dependency pain. After some digging, I tracked down the problem with nerve to the bcrypt module, which is apparently written in C/C++ and has to be compiled after the package manager downloads it.
Unfortunately, it seems like if you want this to work on Windows, the answer is (from one of the bcrypt issues threads) "install a Linux VM". So earlier today I did just that, and started running into other dependencies (you need certain unnamed apt packages installed before you can even think about building, despite GCC being installed), then eventually after seeing yet another C compiler error (about some package or other not being able to find "Arrays.c" I think), I actually gave up, and switched from nerve to express instead. Ironically, the larger and more complicated express installs with npm on Linux and Windows without a single issue.
So, my question is: is there any filter / dependency tracking available that lets you see if a package has additional dependencies besides node core? Because to me the allure of node is "everything in Javascript", and this kind of stuff dispels the illusion quite unpleasantly. In fact, despite having done more than my time working with C/C++, whenever I see a requirement to "make" something these days I generally run in the other direction screaming. :)
The first solution doesn't tell you if a dependency makes the package impure or not. Much better to search for gyp generated output:
find node_modules/ | grep binding.gyp || echo pure
Look out for the "scripts" field in the package.json.
If it contains something like
"scripts": {
"install": "make build",
}
and a Makefile in the root directory, there's a good possibility that the package has some native module which would have to be compiled and built. Many packages include a Makefile only to compile tests.
This check on the package documents does not exclude the possibility that some dependency will have to be compiled and built. That would mean repeating this process for each dependency in the package.json, their dependencies and so on.
That said many modules have been updated to install, without build on Windows, express for one. However that cannot be assured of all packages.
Using a Linux VM seems to be the best alternative. Developing Node.js applications on Window gives you step by step instructions on installing a VM, Node.js and Express.
Node is not "everything javascript" , since one way to extend node core is to write c/c++ plugins.
So Node is more a javascript wrapper around c/c++ modules using V8.
How could you write efficient database drivers in pure javascript for instance ? it would be possible but slow.
as for the filters , it is up to the author to document his package. there is no automatic filter.