Unable to run a node.js file with #babel/preset-env - node.js

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks

followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}

Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

Related

"/" is not recognized as an internal or external command

Today, I'm coding a few npm packages and a few things that need to be prepared repeatedly.
So I wanted to code a CLI to get those things done quickly.
Here is the src/cli.js code:
export function cli(args){
console.log(args);
}
Here is the package.json code:
{
"name": "my-project",
"version": "1.0.0",
"description": "A CLI to bootstrap new project",
"main": "src/index.js",
"bin": {
"#kensoni/my-project": "bin/my-project",
"my-project": "bin/my-project"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"cli"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ken Nguyen",
"license": "MIT",
"dependencies": {
"arg": "^5.0.0",
"esm": "^3.2.25",
"inquirer": "^8.1.1"
}
}
Here is the bin/my-project code:
#!/usr/bin/env/ node
require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
After I execute the command npm link and open a new cmd type my-project, I get the following message:
'"/"' is not recognized as an internal or external command,
operable program or batch file.
I am using these versions:
node: 14.17.1
npm: 7.18.1
Any ideas how it might work.
Thanks in advance.
Remove "/" after env
#!/usr/bin/env node
//...

Node js + TypeSc + Babel "Error: Cannot find module 'koa'"

I have cloned a demo . after installing koa,koa-router etc, I got an error. Here is my index.ts file
import Koa from "koa"
import Router from "koa-router"
import logger from "koa-logger"
import json from "koa-json"
const app = new Koa()
const router = new Router()
router.get("/",async(ctx: any,next: any)=>{
ctx.body = {
meg:"Hello world"
}
await next
})
app.use(logger())
app.use(json())
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000,()=>console.log("app is running at 3000"))
Here is my package.json
{
"name": "babel-typescript-sample",
"version": "0.7.2",
"license": "MIT",
"scripts": {
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"
},
"devDependencies": {
"#babel/cli": "^7.8.3",
"#babel/core": "^7.8.3",
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-env": "^7.8.3",
"#babel/preset-typescript": "^7.8.3",
"#types/koa": "^2.11.3",
"typescript": "^3.7.5"
},
"dependencies": {
"koa": "^2.11.0",
"koa-bodyparser": "^4.3.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.1",
"koa-router": "^8.0.8"
}
}
my file structure loos like:
/-------
-lib
-index.js
-index.d.ts
-node_modules
-src
-index.ts
Actually,before this ,when I run npm run build,I got error src/index.ts:2:20 - error TS2307: Cannot find module 'koa-router'. I write koa-router.d.ts myself, but I don't think it's a great idea, How do you guys resolve?
You have a few options:
Install koa-router typings with npm install --save-dev #types/koa-router.
Set moduleResolution inside tsconfig.json to node. See this for differences. Note that this option only works if the dependency has typings included. In this case, it does not, so first option would be more correct.
Edit paths (points to directory) or types (points to .d.ts) inside tsconfig.json to point to your typings. Yes, writing your typings ("by hand") for existing dependency is generally considered a bad idea, since it could update and therefore break your typings. If a dependency does not have typings, you can contribute to it by generating it using JSDoc, if it uses JavaScript.

Mongo DB driver "Cannot find module 'bson'." and no intellisense

I'm setting up a server, and want to use Mongo DB as my Data storage but I am not getting any intellisence from Visual Studio Code whatsoever and adding and removing flow. When I "CTRL + Click" MongoClient I get 2 same errors i.e. Cannot find module 'bson'. when I hover over bson
it shows the location to AppData/Local/Microsoft/TypeScript/3.4/node_modules/#types/bson/index
could not find module bson on line :
`import { ObjectID, Timestamp } from 'bson';`
and line"
export { Binary, DBRef, Decimal128, Double, Long, MaxKey, MinKey, ObjectID, ObjectId, Timestamp } from 'bson';
What I've tried is installing mode and various npm packages you'll see from my package.json file and running npm install, npm update and installing node - gyp for global but still the error has persisted
package.json
{
"name": "MadLibs_SERVER",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#types/bson": "^4.0.0",
"#types/mongodb": "^3.1.22",
"#types/node": "^11.13.0",
"bson": "^4.0.2",
"express": "^4.16.4",
"mongodb": "^3.2.3",
"node": "^11.13.0"
}
}
Update 1 :
After re installing all modules i.e express and mongodb and removing all others now I still don't get intellisense but opening the file I am getting more errors Cannot find type definition file for 'node'.
now my package has only mongodb and express but still no fix.

unexpected token import in ES2017 with babel and Jest

I try to use Jest with bablejs and ES2017 in my project, according to the Jest Getting Started page and also Bablejs config for ES2017 this is my .babelrc file :
{
"presets": ["es2017"],
"env": {
"test": {
"presets": ["es2017"]
}
}
}
And my package.json is:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": ""
},
"author": "",
"license": "ISC",
"bugs": {
"url": ""
},
"homepage": "",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-jest": "^21.2.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2017": "^6.24.1",
"jest": "^21.2.1"
}
}
When I type npm test to run all my test with jest i get these error :
){import StateList from './StateList';
^^^^^^
SyntaxError: Unexpected token import
It means it doesn't know import.
babel-preset-es2017 does not transform import statements, because it only includes the plugins: syntax-trailing-function-commas and
transform-async-to-generator.
When installing babel-preset-es2017 you also get a warning that it has been deprecated in favour of babel-preset-env, which contains everything that the es201x presets contained and more.
warning babel-preset-es2017#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
As shown in the Migration guide from es2015 to env, it is a drop-in replacement.
npm install --save-dev babel-preset-env
And change your .babelrc to:
{
"presets": ["env"]
}
Do not confuse babel-preset-env with Babel's env option, which I have removed from your current config, since you are using the exact same presets for the test environment as for any other, so it doesn't have any effect.
You can configure babel-preset-env to only transform features that are not supported by the platform you target, for example { "targets": { "node": "current" } } will only transform features that aren't supported by the Node version you are running. If no targets are specified, it will transform everything. For details see the Env preset documentation.
Note: With the upcoming version 7 of Babel, the official packages will be published under the namespace #babel, which means that babel-preset-env will be #babel/preset-env.

How to start my globally installed node application?

I developed small application.
I did this to install developed module globally:
npm install . -g
I got this response:
$ npm install -g .
update-deps-dev#1.0.0 C:\Users\Vladislav.Sharikov\AppData\Roaming\npm\node_modul
es\update-deps-dev
I got my module in place, where other globally installed modules are stored. Also, my module is available by:
npm ls -g --depth=0
Now, I want to start this module from any place on my pc. How should I do this?
I try to run, but getting this:
Vladislav.Sharikov#PC /D/Dev
$ update-deps-dev
sh: update-deps-dev: command not found
I am using Windows 7 x64 + Git Bash.
How should I start my globally installed node application?
My package.json file contents:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Not sure about git bash. But in a cross platform way you can simply define a bin field on your package json to tell npm to generate a binary available on your command line later. It really works fine on windows, mac, linux.
Note that it is a per user binary. If you are working on linux it won t install it into /usr/bin, but depending on your system, more probably ~/node_modules/bin.
Suppose your binary is defined into bin.js file, you just need to add a new Object field bin, and foreach bin you want to define a key, the name of the binary, and its value must point to the path of your bin file relative to the package file:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"update-deps-dev": "./bin.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Read also,
https://docs.npmjs.com/files/package.json#bin
https://docs.npmjs.com/cli/bin

Resources