Unable to run babel-node on Windows - node.js

I would appreciate it if someone could help me figure out what is causing the error mentioned below.
I have a test Express project with a scripts section in package.json that looks like the following:
"scripts": {
"dev": "backpack",
"build": "backpack build",
"test-server": "babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha",
"test-server-ms": "babel-node.cmd node_modules/.bin/babel-istanbul.cmd cover node_modules/.bin/_mocha.cmd"
},
The test-server-ms script is what I am trying to execute:
babel-node.cmd node_modules/.bin/babel-istanbul.cmd cover
node_modules/.bin/_mocha.cmd "test/server"
This yields an error when parsing the babel-istanbul.cmd file:
(function (exports, require, module, __filename, __dirname) { #IF EXIST "%~dp0\node.exe" (
^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\hqrsingh\Development\workspaces\vscode\express-es6-starter\node_modules\babel
-register\lib\node.js:152:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Function.Module.runMain (module.js:604:10)
at Object.<anonymous> (C:\Users\hqrsingh\Development\workspaces\vscode\express-es6-starter\node_modules\babel-cli\lib\_babel-node.js:154:22)
The error occurs when the #IF directive is encountered which is part of a Windows .cmd script in node_modules\.bin\babel-istanbul.cmd:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\babel-istanbul\lib\cli.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\babel-istanbul\lib\cli.js" %*
)
I am kicking all of this off within a Windows 7 Pro command shell via npm:
npm run-script test-server-ms test/server
For reference, here is the entire package.json file:
{
"name": "express-es6-starter",
"version": "2.0.0",
"description": "A complete boilerplate for Express.js development including unit test and code coverage.",
"main": "lib/index.js",
"scripts": {
"dev": "backpack",
"build": "backpack build",
"test-server": "babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha",
"test-server-ms": "babel-node.cmd node_modules/.bin/babel-istanbul.cmd cover node_modules/.bin/_mocha.cmd"
},
"pre-commit": [
"test-server"
],
"dependencies": {
"express": "^4.14.0"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.24.1",
"babel-istanbul": "^0.12.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.16.0",
"babel-preset-stage-2": "^6.24.1",
"backpack-core": "^0.0.7",
"chai": "^3.5.0",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.1.1",
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.4",
"mocha": "^3.2.0",
"phantomjs": "^2.1.7",
"pre-commit": "^1.2.2",
"webpack": "^1.14.0"
}
}

FWIW, I was able to solve this by avoiding the use of the babel-* modules entirely. I still do not know how one would go about executing platform specific scripts via npm or if that should even be encouraged.
Here is my entire package.json file:
{
"name": "express-es6-starter",
"version": "2.0.0",
"description": "A complete boilerplate for Express.js development including unit test and code coverage.",
"scripts": {
"start": "node ./build/main.js",
"clean-build": "rm -rf ./build/*",
"dev": "backpack dev",
"build": "backpack build",
"compile": "babel --presets es2015,stage-0 -d build/ src/",
"test-client-karma": "node ./node_modules/karma/bin/karma start karma.conf.js || exit 0",
"test-client-mocha": "mocha test/client/*.js --require babel-register --reporter spec || exit 0",
"test-server": "mocha --compilers js:babel-core/register test/server/**/*.js",
"test": "npm run-script test-server; npm run-script test-client-mocha"
},
"pre-commit": [
"test-server"
],
"dependencies": {
"express": "^4.14.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"backpack-core": "^0.0.7",
"brfs": "^1.4.3",
"browserify": "^14.3.0",
"browserify-shim": "^3.8.14",
"chai": "^3.5.0",
"child_process": "^1.0.2",
"jasmine-core": "^2.6.2",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.1.1",
"karma-browserify": "^5.1.1",
"karma-html-reporter": "^0.2.7",
"karma-jasmine": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-ng-scenario": "^1.0.0",
"karma-phantomjs-launcher": "^1.0.4",
"mocha": "^3.2.0",
"phantomjs": "^2.1.7",
"pre-commit": "^1.2.2",
"webpack": "^1.14.0",
"watchify": "^3.9.0"
}
}
This is the project structure that the above package.json is based upon:

Related

Cannot dockerize Prisma node mysql application #prisma/client did not initialize yet

I am having lots of trouble resolving this bug. I wrote a prisma nodejs application with react frontend and am ready to deploy. The app is working perfectly on my ubuntu system but everytime i try to run
sudo docker-compose up --build
I run into the same error
app/node_modules/.prisma/client/index.js:3
throw new Error(
^
Error: #prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
In case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issues
at new PrismaClient (/app/node_modules/.prisma/client/index.js:3:11)
at Object.<anonymous> (/app/build/decks.js:10:16)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/app/build/game.js:41:17)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
/app/node_modules/.prisma/client/index.js:3
throw new Error(
no matter what i try
this is my Dockerfile for my node application:
FROM node:16
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/
COPY . .
RUN npm install
EXPOSE 8000
CMD [ "node", "build/server.js" ]
my .env
MYSQL_HOST="localhost"
MYSQL_USER="game"
MYSQL_NAME="mygame"
MYSQL_PASS="mypassword"
MYSQL_ROOT_PASSWORD=mynewpassword
DB_URL="mysql://game:mypassword#localhost:3307/mygame"
DB_LOCAL_PORT=3307
DB_DOCKER_PORT=3308
NODE_LOCAL_PORT=8000
NODE_DOCKER_PORT=8000
and my package.json
{
"name": "nodejsin30min",
"version": "1.0.0",
"description": "",
"main": "./src/server.ts",
"scripts": {
"generate": "npx prisma generate",
"deploy": "npx prisma migrate deploy",
"devStart": "nodemon ./src/server.ts",
"test": "jest",
"build": "tsc -p",
"postinstall": "prisma generate"
},
"prisma": {
"schema": "./prisma/schema.prisma"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#prisma/client": "^4.6.1",
"#socket.io/admin-ui": "^0.2.0",
"#types/jest": "^28.1.7",
"#types/mocha": "^9.1.1",
"bcrypt": "^5.1.0",
"bcrypt-helper": "^0.2.1",
"body-parser": "^1.20.1",
"chroma": "^0.0.1",
"circular-json": "^0.5.9",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.17.1",
"express-jwt": "^7.7.7",
"express-oauth2-jwt-bearer": "^1.2.0",
"express-openid-connect": "^2.9.0",
"express-rate-limit": "^6.7.0",
"flatted": "^3.2.5",
"jwks-rsa": "^2.1.5",
"knex": "^2.3.0",
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
"prisma": "^4.6.1",
"socket.io": "^4.4.0",
"socket.io-client": "^4.4.0",
"sqlite3": "^5.0.9",
"typesync": "^0.9.2",
"underscore": "^1.13.1",
"webpack": "^5.74.0"
},
"devDependencies": {
"#prisma/client": "^4.6.1",
"#types/express": "^4.17.13",
"#types/express-jwt": "^7.4.2",
"#types/jest": "^28.1.7",
"#types/mocha": "^9.1.1",
"#types/mysql": "^2.15.21",
"#types/node": "^18.7.7",
"#types/underscore": "^1.11.4",
"jest": "^27.4.3",
"nodemon": "^2.0.15",
"prisma": "^4.6.1",
"ts-node": "^10.9.1",
"typescript": "^4.4.3"
}
}
As you can see I already tried implementing a posinstall script with no luck. I have already tried shifting around multiple prisma commands (migrate dev, migrate deploy, generate) into every possible position of my Dockerfile with no luck - always the same error. On my device I NEVER had to manually generate a client and before i implemented prisma the application could be built just fine. I have no more ideas what to look for.
any help is appreciated.
Furthermore i habe already tried leaving out enironment variables and instead hardcoding the values, which did not work either
I also tried running RUN npm i -g prisma befor a prisma generate in my Dockerfile

I re-installed node_modules in Nuxt.js and suddenly Must use import to load ES Module:~ appeared

When I re-installed node_modules, I was suddenly told that ufo and node-fetch were missing. After adding them, I ran npm run dev and opened localhost in a browser and encountered this error.
This error also occurred when I cloned this project on another laptop, and I am at a loss as to how to fix it.
I tried to solve the problem by myself as much as possible, but I couldn't find a solution because the stackframe was somehow Missing stack frames and this error itself is an error in the library. I'm not very familiar with tack overflow questions, so please let me know if you find any problems.
error
Must use import to load ES Module: C:\Users\Cou\Documents\就職関係\job_portfolio\nuxt_main\node_modules\.pnpm\node-fetch#3.2.6\node_modules\node-fetch\src\index.js require() of ES modules is not supported. require() of C:\Users\Cou\Documents\就職関係\job_portfolio\nuxt_main\node_modules\.pnpm\node-fetch#3.2.6\node_modules\node-fetch\src\index.js from C:\Users\Cou\Documents\就職関係\job_portfolio\nuxt_main\node_modules\.pnpm\vue-server-renderer#2.6.14\node_modules\vue-server-renderer\build.dev.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\Cou\Documents\就職関係\job_portfolio\nuxt_main\node_modules\.pnpm\node-fetch#3.2.6\node_modules\node-fetch\package.json.
Stack frames
internal/modules/cjs/loader.js:1089:13
Module._extensions..js
internal/modules/cjs/loader.js:937:32
Module.load
internal/modules/cjs/loader.js:778:12
Module._load
internal/modules/cjs/loader.js:961:19
Module.require
internal/modules/cjs/helpers.js:92:18
require
webpack:/external "node-fetch":1:
Object.node-fetch
webpack/bootstrap:25:
__webpack_require__
.nuxt/server.js:1:
Module../.nuxt/server.js
webpack/bootstrap:25:
__webpack_require__
package.json
{
"name": "nuxt_main",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate",
"lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint:style": "stylelint \"**/*.{css,scss,sass,html,vue}\" --ignore-path .gitignore",
"lint:prettier": "prettier --check .",
"lint": "npm run lint:js && npm run lint:style && npm run lint:prettier",
"lintfix": "prettier --write --list-different . && npm run lint:js -- --fix && npm run lint:style -- --fix",
"prepare": "husky install",
"test": "jest"
},
"lint-staged": {
"*.{js,vue}": "eslint --cache",
"*.{css,scss,sass,html,vue}": "stylelint",
"*.**": "prettier --check --ignore-unknown"
},
"dependencies": {
"#mdi/js": "^6.7.96",
"#nuxtjs/axios": "^5.13.6",
"core-js": "^3.19.3",
"csv-loader": "^3.0.3",
"csv-parse": "^5.2.0",
"fs": "0.0.1-security",
"node-fetch": "^3.2.6",
"nuxt": "^2.15.8",
"pnpm": "^7.4.0",
"ufo": "^0.8.4",
"vue": "^2.6.14",
"vue-server-renderer": "^2.6.14",
"vue-template-compiler": "^2.6.14",
"vuetify": "^2.6.1",
"webpack": "^4.46.0"
},
"devDependencies": {
"#babel/eslint-parser": "^7.16.3",
"#nuxtjs/eslint-config": "^8.0.0",
"#nuxtjs/eslint-module": "^3.0.2",
"#nuxtjs/stylelint-module": "^4.1.0",
"#nuxtjs/vercel-builder": "^0.22.1",
"#nuxtjs/vuetify": "^1.12.3",
"#vue/test-utils": "^1.3.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^27.4.4",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-nuxt": "^3.1.0",
"eslint-plugin-vue": "^8.2.0",
"husky": "^7.0.4",
"jest": "^27.4.4",
"lint-staged": "^12.1.2",
"postcss-html": "^1.3.0",
"prettier": "^2.5.1",
"stylelint": "^14.1.0",
"stylelint-config-prettier": "^9.0.3",
"stylelint-config-recommended-vue": "^1.1.0",
"stylelint-config-standard": "^24.0.0",
"vue-jest": "^3.0.4"
}
}
OS: Windows
Node: v14.17.4
Apparently it was due to pnpm not being able to install all the required packages.
Running pnpm install followed by npm install (or yarn install) solved the problem.

Need to increase Node max_old_space_size via my existing package.json

I inherited an application has has begun to get large and I am receiving the error message CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory. Based on my research I need to set the node argument max_old_space_size.
I am currently running Node 7.8.0 so it does not appear I can use the environment variable NODE_OPTIONS, I have tried to set this with no luck. I have pasted my existing package.json file below but I cannot figured out where in the scripts section I can place the argument --max_old_space_size=2048
The node process typically crashes with the above error when the memory gets around 1.6 GB
{
"scripts": {
"lint": "tslint --exclude=node_modules/** **/*.ts",
"generate:client": "ts-node ./swagger/generate-client.ts --baseApiUrl=http://localhost:5003",
"generate:client:prod": "ts-node ./swagger/generate-client.ts --baseApiUrl=%API_URL%",
"start": "npm run clean:dist && npm run generate:client && webpack -w",
"clean:dist": "rimraf ./wwwroot/*",
"build": "npm run clean:dist && npm run generate:client:prod && webpack -p"
},
"dependencies": {
"#types/googlemaps": "^3.26.1",
"#types/handlebars": "^4.0.31",
"#types/node": "^7.0.5",
"#types/react": "^15.0.34",
"#types/react-dom": "^15.5.1",
"#types/react-router": "^3.0.3",
"#types/superagent": "^2.0.36",
"#types/yargs": "^6.6.0",
"assets-webpack-plugin": "^3.5.1",
"css-loader": "^0.26.1",
"extract-text-webpack-plugin": "2.1.2",
"file-loader": "^0.10.0",
"google-map-react": "^0.22.3",
"handlebars": "^4.0.6",
"mobx": "^3.1.0",
"mobx-react": "^4.1.0",
"moment": "^2.17.1",
"node-sass": "^4.5.0",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-resize-observer": "^0.1.0",
"react-router": "^3.0.2",
"rimraf": "^2.6.1",
"sass-loader": "^6.0.1",
"style-loader": "^0.13.1",
"superagent": "^3.4.4",
"ts-loader": "^2.2.2",
"ts-node": "^3.1.0",
"tslint": "^4.4.2",
"tslint-loader": "^3.5.3",
"tslint-react": "^3.0.0",
"typescript": "^2.4.1",
"url-loader": "^0.5.7",
"webpack": "^3.1.0",
"webpack-md5-hash": "^0.0.5",
"yargs": "^6.6.0"
}
}
Update 8/27/2020:
I have attempted to use the proposed solution of:
"scripts": {
"high-memory": "node --max_old_space_size=2048 ./node_modules/.bin/ts-node",
"generate:client": "npm run high-memory -- ./swagger/generate-client.ts --baseApiUrl=http://localhost:5003",
"generate:client:prod": "npm run high-memory -- ./swagger/generate-client.ts --baseApiUrl=%API_URL%",
}
Receiving the following error when running npm start:
C:\Projects\Twiddy\TwiddyOps\Web\node_modules\.bin\ts-node:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
Tell the author that this fails on your system:
npm ERR! node --max_old_space_size=2048 ./node_modules/.bin/ts-node
"./swagger/generate-client.ts" "--baseApiUrl=http://localhost:5003"
That's how I use it:
"scripts": {
"high-memory": "node --max_old_space_size=2048 ./node_modules/.bin/ts-node",
"generate:client": "npm run high-memory -- ./swagger/generate-client.ts --baseApiUrl=http://localhost:5003",
"generate:client:prod": "npm run high-memory -- ./swagger/generate-client.ts --baseApiUrl=%API_URL%",
}

Requiring external babel register error during npm start

I started angular Application via npm start with gulp/babel enabled.
After starting, the browser page keeps loading and is throwing an error "requiring external babel register".
given below logs from terminal:
[19:52:47] Requiring external module #babel/register
[19:52:53] Using gulpfile ~\WebstormProjects\agent-dealer-portal-frontend\portals-integration\front\gulpfile.babel.js
[19:52:53] Starting 'default'...
babel-register is listed properly in the terminal.
C:\Users\vramanathan\WebstormProjects\agent-dealer-portal-frontend\portals-integration\front>npm list babel-register
#globant/cna_national_portal#0.35.0 C:\Users\vramanathan\WebstormProjects\agent-dealer-portal-frontend\portals-integration\front
`-- babel-cli#6.26.0
+-- babel-core#6.26.3
| `-- babel-register#6.26.0 deduped
`-- babel-register#6.26.0
.babelrc file contents:
{
"presets": [
"es2015"
]
}
Package.json:
{
"main": "gulpfile.babel.js",
"name": "#globant/cna_national_portal",
"version": "00.35.00",
"babel": {
"presets": [
"#babel/env"
],
"compact": false
},
......
"devDependencies": {
"#babel/core": "^7.5.5",
"#babel/preset-env": "^7.5.5",
"#babel/register": "^7.5.5",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.18.0",
"del": "^3.0.0",
"eslint": "^5.0.1",
"gulp": "^4.0.2",
"gulp-autoprefixer": "^3.1.1",
"gulp-babel": "^6.1.2",
"gulp-cli": "^1.4.0",
"gulp-concat": "^2.6.1",
"gulp-cssnano": "^2.1.2",
"gulp-eslint": "^4.0.2",
"gulp-header": "^1.8.9",
"gulp-if": "^2.0.2",
"gulp-imagemin": "^2.4.0",
"gulp-load-plugins": "^1.4.0",
"gulp-ng-annotate": "^2.0.0",
"gulp-rev": "^8.1.1",
"gulp-rev-replace": "^0.4.4",
"gulp-sass": "^3.1.0",
"gulp-sass-glob": "^1.0.8",
"gulp-sourcemaps": "^1.11.0",
"gulp-uglify": "^1.5.4",
"gulp-uncss": "^1.0.6",
},
"private": true,
"scripts": {
"start": "gulp",
"build": "gulp build --production",
"test": "gulp test",
"test:watch": "gulp test:watch",
"eslint": "gulp eslint"
}
}
The following may be useful: there is some incompatibilities between certain versions of Node / gulp when that arise when using different OSs (I'm on Chromebook for my work).
For me, downgrading to Node 10 solved my issue (via command): nvm use 10

babel-node vs node: graphql files with import/exports

It appears that NodeJS does not understand import/export commands in graphql files (extension .gql), but babel-node does. The only fix I can come up with is to rename the .gql files to .js files, but I lose syntax highlighting.
Is there a simple fix to have node honor import/exports in files non-js extensions? Attached is my package.json; npm run dev works, but npm run build; npm run start does not:
{
"name": "MyAPI",
"version": "1.0.0",
"description": "MyAPI using GraphQL",
"main": "api/server.js",
"scripts": {
"build": "babel api -d src --copy-files",
"start": "node src/server.js",
"debug": "babel-node --inspect api/server.js",
"dev": "nodemon api/server.js --watch api --watch tests --ext js,gql --exec babel-node",
"lint": "eslint api,tests",
"test": "mocha --require babel-core/register tests"
},
"author": "JML",
"devDependencies": {
"apollo-client": "^2.0.2",
"apollo-client-preset": "^1.0.2",
"babel-eslint": "^7.2.1",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"babel-plugin-transform-async-generator-functions": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"chai": "^4.1.2",
"chai-subset": "^1.6.0",
"eslint": "^4.3.0",
"eslint-plugin-babel": "^4.1.2",
"graphql": "^0.10.5",
"mocha": "^4.0.1",
"node-fetch": "^1.7.3",
"nodemon": "^1.11.0",
"randexp": "^0.4.6"
},
"dependencies": {
"babel-cli": "^6.24.0",
"babel-core": "^6.25.0",
"babel-preset-latest": "^6.24.1",
"babel-runtime": "^6.26.0",
"body-parser": "^1.17.1",
"casual": "^1.5.14",
"cors": "^2.8.4",
"eslint": "^4.10.0",
"express": "^4.15.2",
"graphql-server-express": "1.0.4",
"graphql-tag": "^2.5.0",
"graphql-tools": "^1.1.0",
"lodash": "^4.17.4",
"mysql": "^2.14.1",
"nano": "^6.4.2",
"treeize": "^2.1.2"
}
}
npm install babel-cli babel-preset-env
once you installed those, it will create populated .babelrc file at the root level. you do not need to modify. then you need to load those configurations to the start script in package.json.
"start": "nodemon src/index.js --ext js,graphql --exec babel-node ",
If you do not know about nodemon, u need to install it globally and it will watch the src/index.js file.
--ext js, graphql // this is optional. it will have vscode to colorize the code.
Lastly, you need to configure babel to support object spread operator.
npm install babel-plugin-transform-object-rest-spread
then add this "transform-object-rest-spread" to the plugins array in the .babelrc file.
.babelrc
{
"presets": ["env", "react", "stage-0"],
"plugins": [
"transform-class-properties",
"transform-decorators",
"transform-react-constant-elements",
"transform-react-inline-elements",
"transform-object-rest-spread"
]
}

Resources