Good day:
I've just installed Babel Preset-ENV through NPM and getting this issue:
[nodemon] starting `babel-node server.js server.js`
/home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:328
throw e;
^
TypeError: Cannot read property 'loose' of undefined (While processing preset: "/home/vagrant/api/node_modules/#babel/preset-env/lib/index.js")
at _default (/home/vagrant/api/node_modules/#babel/plugin-transform-modules-commonjs/lib/index.js:19:22)
at Function.memoisePluginContainer (/home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)
at Function.normalisePlugin (/home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32)
at /home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
at Array.map (native)
at Function.normalisePlugins (/home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
at OptionManager.mergeOptions (/home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
at /home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:265:14
at /home/vagrant/api/node_modules/babel-core/lib/transformation/file/options/option-manager.js:323:22
at Array.map (native)
My Package.JSON is the following:
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon -L server.js --ignore 'db/schema.json' --exec babel-node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "^7.0.0-beta.32",
"#babel/preset-env": "^7.0.0-beta.32",
"babel-core": "^6.26.0",
"express": "^4.16.2",
"express-graphql": "^0.6.11",
"express-oauth-server": "^2.0.0",
"graphql": "^0.11.7",
"jsonwebtoken": "^8.1.0",
"mongo": "^0.1.0",
"mongoose": "^4.13.2",
"password-hash": "^1.2.2",
"react-relay": "^1.4.1"
},
"devDependencies": {
"babel-cli": "^6.26.0"
}
}
And also my .babelrc is this:
vagrant#vagrant-ubuntu-trusty-64:~/api$ cat .babelrc
{
"presets": [
["#babel/preset-env", {
"targets": {
"node": "current"
}
}]
]
}
Before the upgrade I was using stage-0 and es2015 presets that were working however, after the upgrade this has been buggy. Note, my NPM version is 4.6.1.
Thanks.
Make sure your Babel dependencies are compatible with each other.
this:
"#babel/core": "^7.0.0-beta.32",
"#babel/preset-env": "^7.0.0-beta.32",
"babel-core": "^6.26.0",
"babel-cli": "^6.26.0"
should be this:
"#babel/cli": "^7.0.0-beta.34",
"#babel/core": "^7.0.0-beta.34",
"#babel/node": "^7.0.0-beta.34",
I followed this instruction for setup https://github.com/babel/babel/tree/master/experimental/babel-preset-env however, the documentation here seems outdated. I was advised to use the official babel documentation mentioned here for the preset setup https://babeljs.io/
Related
So I am simply trying to deploy my node server to Heroku cloud. It is crashing because of the ES modules not supported issue. The specific error I am getting is
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/app/src/routes' is not supported resolving ES modules imported from /app/src/server.js
I am trying to import my routes directory. I have read the solutions on Stack related to this issue, and none of them are working for me. For example inlcuding "type":"module" in my package.json, is NOT working.
What's going on? It runs perfectly locally using 'npm run dev' script.
Ironman
package.json
{
"name": "back-end",
"type": "module",
"version": "1.0.0",
"description": "",
"engines": {
"node": "14.x"
},
"main": "index.js",
"scripts": {
"dev": "nodemon --exec babel-node -r dotenv/config ./src/server.js",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node -r dotenv/config ./src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "7.13.10",
"#babel/node": "7.13.12",
"#babel/preset-env": "7.13.12",
"#sendgrid/mail": "^7.6.0",
"axios": "^0.24.0",
"bcrypt": "^5.0.1",
"dotenv": "^10.0.0",
"esm": "^3.2.25",
"express": "4.17.1",
"googleapis": "^92.0.0",
"jsonwebtoken": "^8.5.1",
"mongodb": "3.6.5",
"uuid": "^8.3.2"
},
"devDependencies": {
"nodemon": "2.0.7"
}
}
The answer, the solution, was to require the esm package on startup:
"start": "node -r esm -r dotenv/config ./src/server.js"
It looks like import modules errors.
This is my package.json settings, can anyone point me what am I missing ?
Thanks.
{
"name": "prj",
"version": "1.0.0",
"scripts": {
"dev": "vite --port 8080",
"build": "vite build",
"serve": "vite preview --port 8000",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest"
},
"jest": {
"moduleFileExtensions": ["js", "json", "vue"],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "vue-jest"
}
},
"dependencies": {
"axios": "^0.21.1",
"vue": "^3.2.6",
"vue-i18n": "^9.1.7",
"vue-router": "^4.0.11",
"vuex": "^4.0.2"
},
"devDependencies": {
"#babel/core": "^7.15.5",
"#vitejs/plugin-vue": "^1.6.0",
"#vue/compiler-sfc": "^3.0.5",
"#vue/test-utils": "^2.0.0-rc.14",
"babel-jest": "^26.6.3",
"vite": "^2.5.1",
"vue-jest": "^5.0.0-alpha.10"
}
}
ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use node --trace-warnings ... to show where the warning was created)
FAIL src/tests/examples.test.js
● Test suite failed to run
Jest encountered an unexpected token
......
Details:
/some/path/src/tests/examples.test.js:4
import HomePage from './src/views/HomePage.vue';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
I am using webpack and had the same error that I fixed installing babel-core bridge:
npm install --save-dev babel-core#^7.0.0-bridge.0
I needed also the following one in jest.config.js:
moduleNameMapper: {
'^#/(.*)$': '<rootDir>/src/$1',
},
Documentation
To clarify the issue:
I have built a node server, which works fine. But on pushing to git and cloning elsewhere, it doesn't work in the new location.
Cleared node_modules, and tried npm install.
gitignore has only node_modules directory in it.
package.json contains the following
{
"name": "node-todo-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server/server.js",
"test": "export NODE_ENV=test || SET NODE_ENV=test && mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
},
"engines": {
"node": "6.2.2"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"jsonwebtoken": "^8.3.0",
"lodash": "^4.15.0",
"mongodb": "^2.2.5",
"mongoose": "^4.5.9",
"validator": "^5.6.0"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^5.2.0",
"nodemon": "^1.10.2",
"supertest": "^3.1.0"
}
}
I'm a newbie nodejs. I have a trouble about deploying nodejs app to google app engine. This is my error, I have been trying to fix it but it's not work. I've installed babel.
Bug
Updating service [default]...failed.
ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
restaurant_api#1.0.0 prestart /app
npm run -s build
You have mistakenly installed the babel package, which is a no-op in Babel 6.
Babel's CLI commands have been moved from the babel package to the babel-cli package.
npm uninstall babel
npm install --save-dev babel-cli
This is my package.json
"main": "dist",
"scripts": {
"dev": "NODE_ENV=development nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"",
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "NODE_ENV=production pm2 start dist",
"prestart": "npm run -s build",
"lint": "eslint src",
"test": "echo \"Error: no test specified\" && exit 1",
"babel-version": "babel --version"
},
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"node": true
},
"rules": {
"no-console": 0,
"no-unused-vars": 1
}
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"body-parser": "^1.17.0",
"express": "^4.15.0",
"express-jwt": "^5.1.0",
"jsonwebtoken": "^7.3.0",
"mongoose": "^4.8.5",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.0.0",
"pm2": "^2.4.2"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-eslint": "^7.1.1",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-0": "^6.22.0",
"eslint": "^3.16.1"
}
Problem is quite simple, if you notice clearly GAE, does not install dev-dependencies. So move your dev-deps above inside deps , like I did and babel no found error is gone.
{
"name": "scraping",
"version": "0.3.0",
"description": "Starter project for an ES6 RESTful Express API",
"main": "dist",
"scripts": {
"dev": "nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"",
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "node dist",
"prestart": "npm run -s build",
"test": "eslint src"
},
"eslintConfig": {
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"node": true
},
"rules": {
"no-console": 0,
"no-unused-vars": 1
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/developit/express-es6-rest-api.git"
},
"author": "Saransh Sharma <jason#developit.ca>",
"license": "MIT",
"dependencies": {
"body-parser": "^1.13.3",
"compression": "^1.5.2",
"cors": "^2.7.1",
"express": "^4.13.3",
"morgan": "^1.8.0",
"resource-router-middleware": "^0.6.0",
"#jonstuebe/scraper": "^0.1.4",
"babel-cli": "^6.9.0",
"babel-core": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"eslint": "^3.1.1",
"nodemon": "^1.9.2"
},
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"eslint": "^3.1.1",
"nodemon": "^1.9.2"
},
"bugs": {
"url": "https://github.com/developit/express-es6-rest-api/issues"
},
"homepage": "https://github.com/developit/express-es6-rest-api#readme",
"keywords": [
"scraper",
"product",
"from",
"amazon"
]
}
The clue is in the error message.
In your devDependencies section, you already have babel-cli included.
However, your dependencies section still has a reference to "babel": "^6.23.0".
Either remove this line, or replace it with babel-cli, leaving you with:
"dependencies": {
"babel-cli": "^6.23.0",
"body-parser": "^1.17.0",
"express": "^4.15.0",
"express-jwt": "^5.1.0",
"jsonwebtoken": "^7.3.0",
"mongoose": "^4.8.5",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.0.0",
"pm2": "^2.4.2"
},
You need to install babel-cli globally, i.e. run the command "npm install babel-cli -g" as part of your build npm script, or create a pre-install script that install it if you want to keep it more organized in case you need to add more things later on.
"scripts": {
"dev": "NODE_ENV=development nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"",
"build": "npm install bable-cli -g && babel src -s -D -d dist --presets es2015,stage-0",
I tried deploying Node.js API on Heroku, but shows Application Error.
So, used the "heroku local web" command to test any error..and got an error saying that 'babel-node' is not recognized as an internal or external command, operable program or batch file.
On the other hand, when I run the command 'npm start' I dont get any error and the server starts running.
package.json
{
"name": "apollo-starter-kit",
"version": "0.1.0",
"description": "Minimal set of code to write a GraphQL server with Apollo graphql-tools",
"scripts": {
"start": "nodemon ./server.js --exec babel-node",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/apollostack/apollo-starter-kit.git"
},
"keywords": [
"Node.js",
"Javascript",
"GraphQL",
"Express",
"Apollo",
"Meteor"
],
"author": "Jonas Helfer <jonas#helfer.email>",
"license": "MIT",
"bugs": {
"url": "https://github.com/apollostack/apollo-starter-kit/issues"
},
"homepage": "https://github.com/apollostack/apollo-starter-kit#readme",
"dependencies": {
"apollo-server": "^0.1.2",
"casual": "^1.5.10",
"cors": "^2.8.1",
"express": "^4.13.4",
"lodash": "^4.17.4",
"mongoose": "^4.8.1",
"sequelize": "^3.30.2",
"sqlite": "^2.3.0"
},
"devDependencies": {
"babel-cli": "6.5.1",
"babel-core": "^6.5.2",
"babel-eslint": "^6.0.0-beta.6",
"babel-loader": "6.2.3",
"babel-plugin-inline-import": "^2.0.1",
"babel-polyfill": "6.5.0",
"babel-preset-es2015": "6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "6.5.0",
"casual": "^1.5.10",
"eslint": "^2.4.0",
"eslint-config-airbnb": "^6.1.0",
"eslint-plugin-import": "^1.1.0",
"eslint-plugin-react": "^4.2.3",
"graphql": "^0.6.0",
"nodemon": "^1.9.1"
},
"peerDependencies": {
"graphql": "^0.5.0 || ^0.6.0"
},
"eslintConfig": {
"parser": "babel-eslint",
"extends": [
"airbnb/base",
"plugin:import/errors"
],
"rules": {
"no-use-before-define": 0,
"arrow-body-style": 0,
"dot-notation": 0,
"no-console": 0
},
"env": {
"mocha": true
}
}
}
I think the most likely cause is that babel is part of dev dependencies and those are not installed by default via official node.js buildpack. Change NPM_CONFIG_PRODUCTION to false and it should work.
You can use command line
heroku config:set NPM_CONFIG_PRODUCTION=false
You can always login into Heroku dyno to check if everything is installed properly
heroku run bash
You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.
ref: https://medium.com/#Cuadraman/how-to-use-babel-for-production-5b95e7323c2f