I am unable to use es-15 with node.js I dont think that there is any problem with script as it was working before.
Package.json is below
{
"name": "server",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "nodemon index.js --exec babel-node --presets es2015, stage-2"
},
"license": "MIT",
"dependencies": {
"express": "^4.16.4",
"express-graphql": "^0.7.1",
"graphql": "^14.0.2",
"mongoose": "^5.3.8"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"nodemon": "^1.18.5"
}
}
On running npm start, I get the error below,
> server#1.0.0 start /home/ansh/boo/server
> nodemon index.js --exec babel-node --presets es2015, stage-2
[nodemon] 1.18.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node index.js --presets es2015, stage-2`
/home/ansh/boo/server/node_modules/babel-core/lib/transformation/file/options/option-manager.js:328
throw e;
^
Error: Couldn't find preset "" relative to directory "/home/ansh/boo/server"
at /home/ansh/boo/server/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
at OptionManager.resolvePresets (/home/ansh/boo/server/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (/home/ansh/boo/server/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (/home/ansh/boo/server/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (/home/ansh/boo/server/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at compile (/home/ansh/boo/server/node_modules/babel-register/lib/node.js:103:45)
at loader (/home/ansh/boo/server/node_modules/babel-register/lib/node.js:144:14)
at Object.require.extensions.(anonymous function) [as .js] (/home/ansh/boo/server/node_modules/babel-register/lib/node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:605:32)
[nodemon] app crashed - waiting for file changes before starting...
Related
I am trying to create and run a simple TypeScript NodeJS project. Followings are my codes:
server.ts:
import express from 'express';
const app = express();
const PORT = 3000;
app.listen(PORT , ()=>{
console.log('Server is listening on port $PORT');
});
tsconfig.json:
{
"compilerOptions": {
"target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"outDir": "./build", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
package.json:
{
"name": "back",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon build/server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"#types/express": "^4.17.13",
"express": "^4.18.1",
"mongoose": "^6.5.0",
"nodemon": "^2.0.19"
}
}
When I try npm start I get the following error message:
> back#1.0.0 start
> nodemon build/server.js
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node build/server.js index.js`
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'C:\Users\A\\Test\back\index.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: []
}
[nodemon] app crashed - waiting for file changes before starting...
You've set your entry file to be index.js in package.json, so nodemon is trying to run both server.js and index.js, which does not exist. The simplest way you can solve this − provided that you've transpiled your TS files with tsc, for example − is to change the entry point main in package.json to build/server.js.
So what you have to do:
Install typescript to be able to transpile you code:
npm install typescript
Before running npm start, you have to make sure your code is transpiled, that is, "compiled" to vanilla JavaScript, so add this script to your package.json:
{
"scripts": {
// ...
"build": "tsc"
}
}
Update main in package.json to be build/server.js.
Run npm run build then finally npm start.
Done! Your server will start.
My tips before the answer update:
Also, creating a configuration file for nodemon (nodemon.json) will make things much simpler for you, take a look at the docs.
Lastly, if you're new at this, I recommend taking a look at some ready-made tsconfig bases from this repo, in special this one for Node 16.
I modified my tsconfig.json file as following and it worked:
{
"name": "back",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon src/server.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"#types/express": "^4.17.13",
"express": "^4.18.1",
"mongoose": "^6.5.0"
},
"devDependencies": {
"nodemon": "^2.0.19",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
}
}
The created changes are in these lines:
"main": "index.js" To "main": "server.js"
"start": "nodemon build/server.js" To "start": "nodemon src/server.ts"
Using node.js nodemon, express, and lib.
OS: Windows 10 Home x64
Node.JS Version: Lts
What I want to accomplish: ExpressJS port running.
What's going wrong: an internal file, but most of the time its one of my code files going wrong (i dont know yet)
Console error:
C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server>npm start
> stripe-server#1.0.0 start C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server
> node lib/index.js
C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server\node_modules\express\lib\application.js:355
return this.settings[setting];
^
TypeError: Cannot read property 'case sensitive routing' of undefined
at Object.set (C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server\node_modules\express\lib\application.js:355:25)
at Object.enabled (C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server\node_modules\express\lib\application.js:422:23) at Object.lazyrouter (C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server\node_modules\express\lib\application.js:140:27)
at Object.use (C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server\node_modules\express\lib\application.js:214:8)
at Object.<anonymous> (C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server\lib\api.js:28:23)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! stripe-server#1.0.0 start: `node lib/index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the stripe-server#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Aawesome\AppData\Roaming\npm-cache\_logs\2021-01-11T16_57_07_076Z-debug.log
C:\Users\Aawesome\Documents\Coding\Stripe App\Stripe-Server>
Directory image:
(Imugur Image)
Package.json:
{
"name": "stripe-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"start": "node lib/index.js",
"dev": "concurrently \"tsc -w\" \"nodemon lib/index.js\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"firebase-admin": "^8.6.0",
"stripe": "^8.43.0"
},
"devDependencies": {
"#types/express": "^4.17.6",
"#types/cors": "^2.8.6",
"concurrently": "^5.2.0",
"nodemon": "^2.0.3",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
}
}
This error indicates that this.settings is undefined. Based on the small amount of information provided here, the assumption is that this.settings is an object and this TypeError is thrown because you can't "look up" a key of name 'case sensitive routing' on undefined. Make sure this.settings is an object with a key 'case sensitive routing' before line 355.
I'm trying to put my application into production. But, the teacher who was teaching put it for me, the lib 'sucrase' to use syntax of "import from" instead of "require". Only now Heroku is complaining that he doesn't understand the syntax "import". Is there anything I can do about line of code and configuration? Or do I have to change all "import from" to "require"? ... Lower the logs ...
logs in Heroku
2020-06-27T20:21:41.876688+00:00 app[web.1]: /app/src/server.js:1
2020-06-27T20:21:41.876689+00:00 app[web.1]: import app from './app';
2020-06-27T20:21:41.876690+00:00 app[web.1]: ^^^^^^
2020-06-27T20:21:41.876690+00:00 app[web.1]:
2020-06-27T20:21:41.876691+00:00 app[web.1]: SyntaxError: Cannot use import statement outside a module
2020-06-27T20:21:41.876691+00:00 app[web.1]: at wrapSafe (internal/modules/cjs/loader.js:1054:16)
2020-06-27T20:21:41.876692+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1102:27)
My package.json
{
"name": "futs",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "nodemon src/server.js",
/ ^ ^ ^ Below at the code is a configuration about the 'nodemon' that the teacher gave me
"dev:debug": "nodemon --inspect src/server.js"
},
"dependencies": {
"aws-sdk": "^2.704.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"date-fns": "^2.14.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"multer": "^1.4.2",
"multer-s3": "^2.9.0",
"nodemailer": "^6.4.10",
"pg": "^8.2.1",
"sequelize": "^5.21.13",
"sequelize-cli": "^5.5.1",
"uuid": "^8.1.0",
"yup": "^0.29.1"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.3",
"nodemon": "^2.0.4",
"prettier": "^2.0.5",
"sucrase": "^3.15.0"
}
}
nodemon.json
{
"execMap":{
"js": "node -r sucrase/register"
}
}
I don't know how to run to deploy, if anyone can help me
As you see in the error log : Cannot use import statement outside a module.
import statements are permitted only in ES modules not CommonJS see more about that here :
However you can make Node treat your file as a ES module you need to :
add "type": "module" to package.json .
Add --experimental-modules flag upon running your app(not necessary with Node 13.2.0+ so check your node version with node --version)
as follow :
// package.json
{
"type": "module"
}
take a look here for more information .
OR :
Alternatively you can use the .mjs instead of .jsextension .
SOURCES :
SyntaxError: Cannot use import statement outside a module
https://nodejs.org/api/esm.html .
https://nodejs.org/api/esm.html#esm_enabling .
https://nodejs.org/api/esm.html#esm_code_import_code_statements
To build my app using sucrase I use:
sucrase ./src -d ./dist --transforms imports
This is my package.json file:
"scripts": {
"dev": "nodemon src/server.js",
"build": "sucrase ./src -d ./dist --transforms imports",
"start": "node dist/server.js"
},
Very strange error that I cannot seem to solve.
React-native and node.js and VS Code
To get VS Code to get this far I used the line, yarn add babel-cli
Error I'm Getting:
Debugger listening on ws://127.0.0.1:39061/61f6ad51-d255-463a-a0ad-b686be9b8b0d
Object {dataReducer: Object, reducerSPY: Object, userApp: Object}
home.js:31
Error: Cannot find module 'StyleSheet'
module.js:555
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.get StyleSheet [as StyleSheet] (c:\AndroidEverything\AndroidStudioProjects\Property2\AwesomeProject\node_modules\react-native\Libraries\react-native\react-native-implementation.js:98:29)
at Object.<anonymous> (c:/AndroidEverything/AndroidStudioProjects/Property2/AwesomeProject/app/components/home.js:195:16)
Strange because:
My app runs fine
If I comment out this line all other imports are found, just
StyleSheet.
All files are as I expected in its location. This seems buggy?
home.js(which is called by App.js)
import {
FlatList,
View,
Text,
ActivityIndicator,
StyleSheet
} from 'react-native';
launch.json
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/App.js",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
},
package.json
{
"name": "AwesomeProject",
"version": "0.1.0",
"private": true,
"devDependencies": {
"jest-expo": "25.0.0",
"react-native-scripts": "1.11.1",
"react-test-renderer": "16.2.0"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"babel-cli": "^6.26.0",
"expo": "^25.0.0",
"react": "16.0.0",
"react-dom": "16.0.0",
"react-native": "0.52.0",
"react-navigation": "^1.5.1",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"save": "^2.3.2"
}
}
Any ideas where to keep digging? launch.json file? connectivity? yarn? or a spelling error?
UPDATE:
Removed the line about babel since i'm using yarn for node.js and Expo to run on android. error has changed to:
path\Property2\AwesomeProject\App.js:1
(function (exports, require, module, __filename, __dirname) { import React,
{ Component } from 'react';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:152:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
using yarn upgrade jest-expo#25.1.0-beta.0
warning "expo > react-native-maps#0.19.0" has incorrect peer dependency
"react-native#0.51.0".
warning " > react-native#0.52.0" has incorrect peer dependency
"react#16.2.0".
warning "jest-expo > babel-jest#22.4.3" has unmet peer dependency "babel-
core#^6.0.0 || ^7.0.0-0".
[4/4] Rebuilding all packages...
success Saved lockfile.
success Saved 1 new dependency.
└─ jest-expo#25.1.0-beta.0
warning "jest-expo" is already in "devDependencies". Please remove existing
entry first before adding it to "dependencies".
This is the minimal package.json file of my react native sample project just created to test your cases. Basically you are using CRNA (create-react-native-app) to create your project this is a expo client. See my package.json file below:
{
"name": "AwesomeProject",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.13.1",
"jest-expo": "26.0.0",
"react-test-renderer": "16.3.0-alpha.1"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "jest"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^26.0.0",
"react": "16.3.0-alpha.1",
"react-native": "0.54.0"
}
}
You don't need to add babel-cli for basic project read its usage here but if you still need then add it to your dev-dependencies. Also react-dom you still not need because its for reactjs projects not for react-native.
My suggestion is try to read before you start your project and go step by step then you'll not stuck in some irrelevant issue.
What node version are you using? I am asking this because in your question you mentioned the error the code is throwing
path\Property2\AwesomeProject\App.js:1
(function (exports, require, module, __filename, __dirname) { import React,
{ Component } from 'react';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:152:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
and it seems like code is having trouble resolving import statements.So it may be that yout babel is not able resolve this new syntax.
I´m setting up a shared module environment using node. Here is my directory structure:
project
|--common
| |--package.json
| |--graphql
| |----schema.js
|
|--server
|--package.json
|--server.js
Linking both projects:
$ cd project\common
$ npm link
Then:
$ cd ../server
$ npm link common
common Package.json file:
{
"name": "common",
"private": true,
"version": "3.0.0",
"description": "Common code for all projects",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "MIT"
}
server package.json file:
{
"name": "server",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "concurrently \"babel-node start-server\" \"babel-node start-client\"",
"server": "nodemon --exec \"babel-node start-server.js\"",
"client": "nodemon --exec \"babel-node start-client.js\"",
"lint": "eslint ."
},
"dependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.17.2",
"common": "file:../common",
"connect-mongo": "^2.0.0",
"crypto": "^1.0.1",
"express": "^4.15.3",
"express-graphql": "^0.6.12",
"graphql": "^0.13.1",
"graphql-relay": "^0.5.4",
"jwt-simple": "^0.5.1",
"mongoose": "^5.0.10",
"morgan": "^1.8.2",
"nodemailer": "^4.6.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"path": "^0.12.7",
"validator": "^9.1.1"
},
"devDependencies": {
"concurrently": "3.5.1",
"eslint": "^4.18.2",
"eslint-config-airbnb": "16.1.0",
"eslint-plugin-import": "2.9.0",
"eslint-plugin-jsx-a11y": "6.0.3",
"eslint-plugin-react": "7.7.0",
"fs-extra": "^5.0.0",
"node-fetch": "^2.1.1",
"nodemon": "^1.11.0"
},
"babel": {
"presets": [
"es2015",
"stage-0",
"es2017"
],
"plugins": [
"transform-runtime"
]
}
}
Server.js code:
import schema from "common/graphql/schema";
...
Running server application:
$ npm run server
import { GraphQLSchema } from 'graphql';
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at loader (D:\project\server\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\project\server\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:/9. DEV/WORKSPACE/amplifactory/server/routes.js:25:1)
at Module._compile (module.js:570:32)
at loader (D:\project\server\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\project\server\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
[nodemon] app crashed - waiting for file changes before starting...
From what I´ve seen that is not transpiling code outside server directory, but without putting common ouside server directory I cannot build my shared code.
How to solve this babel issue and make it transpile correctly ?
You aren't going to like it, and maybe there is a better answer in spring 2018, but you may need to have a separate build step for your common code. I have a similar project where the package.json file for the common code looks something like this:
{
"name": "stripmall_gcloud_services",
"version": "1.0.0",
"description": "wraps up some commonly used google helpers",
"main": "./dist/index.js",
"scripts": {
"test": "standard --fix && mocha -r babel-register",
"build": "babel lib -d dist"
}...}
Notice the npm build step that transpiles the common code, and notice that the main key points to the index.js file in the transpiled directory. You just run npm run build whenever you update your common code and all your linking will work as you would expect.