import React from 'react' SyntaxError: Unexpected identifier - javascript

My problem is that when I run "node index.js" I get this message:
Users/martina/JPMC-tech-task-2-PY3/index.js:1
import React from 'react';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:877:16)
at Module._compile (internal/modules/cjs/loader.js:928:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:994:10)
at Module.load (internal/modules/cjs/loader.js:813:32)
at Function.Module._load (internal/modules/cjs/loader.js:725:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1046:10)
at internal/main/run_main_module.js:17:11
this seems to say that there is a problem with the import on the first line of my index.js file, but i have no idea how to solve this problem. thank u if anyone can help!
my package.json :
{
"name": "jpmc-tech-task-2-py3",
"version": "1.0.0",
"description": "s",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start:dev": "webpack-dev-server --config webpack.config.js"
},
"repository": {
"url": "git+https://github.com/insidesherpa/JPMC-tech-task-2-PY3.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/insidesherpa/JPMC-tech-task-2-PY3/issues"
},
"homepage": "https://github.com/insidesherpa/JPMC-tech-task-2-PY3#readme",
"type": "module"
}
basically what i'm being asked to do is that while having an http server on another terminal window i'm supposed to do "npm start" on this terminal and it should open a localhost web page with some data. (it's an assignment but i don't understand much of it) however npm start is not in my package.json by default and i saw that it seems to just be supposed to throw index.js, so that's what i'm trying to do but failing

Node doesn't support es6 import syntax, or didn't until recently.
I'm not exactly sure what your needs are, but here's some options:
Use a new version of node and .mjs. Check out this thread.
use require instead.
I've also had some success with the esm package.

Try running npm run start:dev
items in the script array of package.json are run by using the command npm run before the name of the script.

Related

Running nodemon causes "Error: Cannot find module" error, but is checking the wrong path?

I have a Node.js project where I'm using nodemon. I have a package.json script to start an Express server with nodemon, but when I run it I get an error that the module wasn't found. The package is definitely installed (I see it in /node_modules) but the error seems to be looking in the wrong directory?
package.json:
{
"name": "starter-weatherzen-back-end",
"version": "1.0.0",
"description": "Starter code for WeatherZen backend",
"main": "src/app.js",
"scripts": {
"start": "node src/server.js",
"start:dev": "nodemon src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.21.12",
"pg": "^8.5.1"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
Running "npm run start:dev" will throw an error:
Error: Cannot find module 'C:\Users\name\Desktop\Projects_SE\course\nodemon\bin\nodemon.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
The actual directory of the project is:
C:\Users\name\Desktop\Projects_SE\course\backend
with the module being installed in:
\backend\node_modules
Any help would be much appreciated.
Edit: Forgot to mention in case it's relevant, but running the normal start script (npm start) runs the server just fine.
Figured it out after reading elsewhere about potential issues with having a folder in your path with spaces in its name. Even though the folder isn't directly referenced anywhere in the code, I at one point renamed a folder to include a space. After getting rid of the space, nodemon is able to be found fine. Weirdly the folder wasn't even renamed back to its original name, still different, just without a space.

trying to run npm start on a discord bot but i keep getting a 'node:events' error. While using node v16.13.1

package.json
{
"name": "5ambot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^13.5.0",
"dotenv": "^10.0.0"
},
"devDependencies": {
"nodemon": "^2.0.15"
}
}
when i run npm start i gett an error in internal/modules/cjs/loader.js:892
error
Error: Cannot find module 'node:events'
Require stack:
- DIR\node_modules\discord.js\src\client\BaseClient.js
- DIR\node_modules\discord.js\src\index.js
- DIR\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
at Function.Module._load (internal/modules/cjs/loader.js:745:27)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (DIR\node_modules\discord.js\src\client\BaseClient.js:3:22)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'DIR\\node_modules\\discord.js\\src\\client\\BaseClient.js',
'DIR\\node_modules\\discord.js\\src\\index.js',
'DIR\\index.js'
]
}
i tried reinstalling package-lock:
-rm -rf node_modules package-lock.json && npm install && npm start
didn't work. I am out of ideas and everyone says its cuz the node version is too low and i saw no other solution. Please help
edit: i came to a conclusion that its the discord.js that isn't loading correctly with npm install discord.js
try
Delete node_modules
run
npm cache clean --force
npm install
npm run start
or try yarn
yarn

npm ERR Refusing to install package with name <packageName> under a package also called <packageName>

I am trying to use the jsonfile package in my project, but I get the following errors:
Refusing to install package with name <packagename> under a package also called <packagename>. (Note that none of the directory or filename is same as package name)
Cannot find module <packagename>.
The problem is caused when the name of project in package.json is the same as the module you're trying to install.
To solve this problem, please change the project name in package.json to something else. For example, "jsonfile-test":
{
"name": "jsonfile-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
I think it should be:
npm install --save json-file
not
npm install --save jsonfile
ref: https://www.npmjs.com/package/json-file
If that does not work please try enabling permissions install - e.g. (on mac- but will be equivalent on windows, check: https://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/) run:
sudo npm install --save json-file

Next.js: Module build failed: Error: Cannot find module 'react-hot-loader/babel' from '/home/ugurkaya/Desktop'

I am trying to get started with Next.js, yet I receive the following error:
Module build failed: Error: Cannot find module 'react-hot-loader/babel' from '/home/ugurkaya/Desktop'
The following is my package.json:
"name": "hello-next",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "^6.0.4-canary.6",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-hot-loader": "next"
}
I added the react-hot-loader dependency manually when I get this error, yet it did not solve. Looking forward your helps!
I fixed this issue by just
npm install react-hot-loader --save-dev
This issue is generate because react not find react-hot-loader in dev dependancy
You can solve this problem you just need to install react-hot-loader as dev dependancy
npm install react-hot-loader --save-dev

Module.js failed to load server.js

I made a little project using node.js to do a few things with Discord. It worked when I ran it locally with node server.js and npm start. I decided to put my project online to see if a certain feature worked. When I uploaded it, the main javascript file (server.js) that starts everything wasn't found.
I just want to be clear that Module.js is not something I made, but part of the node.js built in modules. It's the thing that makes the require function what it is.
The error in all it's glory:
module.js:472
throw err;
^
Error: Cannot find module '/var/lib/openshift/583e028e2d52714d2500041/approot/runtime/repo/server.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
Package.json:
{
"name": "app",
"version": "0.0.1",
"description": "test",
"scripts": {
"start": "node --use_strict server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"main": "server.js",
"keywords": [
"discord",
],
"author": "kert",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"discord.js": "7.0.0"
},
"engines" : {
"node" : "6.9.1"
}
}
Structure of files:
Discord
└─── .git
└─── node_modules
└─── rbservers <-nothing is used in this folder
└─── app.js
└─── package.json
└─── server.js
If anyone can point out what stupid thing I'm forgetting, pray tell.
My cartridge on OpenShift is the 'Node.js Auto-Updating' one.
Apparently, git wasn't staging/committing all the files in order for them to be pushed. I'll have to look around on how to fix this.
Ugh.

Categories

Resources