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

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.

Related

I cant import with jest in vue

im trying to learn testing but i get this problem. It seem that jest can not import modules. I have tried a lot of configurations but i couldn't solve it
Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/home/alain-maza/Documentos/entrenamiento/vue/04-pokemon/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import axios from "axios";
| ^
2 |
3 | const pokemonApi = axios.create({
4 | baseURL: 'https://pokeapi.co/api/v2/pokemon'
this is my jest.config
module.exports = {
preset: '#vue/cli-plugin-unit-jest',
}
this is my babel.config
module.exports = {
presets: [
'#vue/cli-plugin-babel/preset'
]
}
and my package.json
{
"name": "04-pokemon",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit"
},
"dependencies": {
"axios": "^1.0.0",
"core-js": "^3.8.3",
"vue": "^3.2.13"
},
"devDependencies": {
"#babel/core": "^7.19.3",
"#babel/preset-env": "^7.19.4",
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-unit-jest": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"#vue/test-utils": "^2.0.0-0",
"#vue/vue3-jest": "^27.0.0-alpha.1",
"babel-jest": "^27.5.1",
"jest": "^27.0.5"
}
}
heklpme please! i have trried a lot of things

error TS2305: Module '"tls"' has no exported member 'TLSSocketOptions'

I'm trying to connect to MongoDB using the MongoDB drivers in a Node.js project but am having issues compiling, and receive the titular error "node_modules/mongodb/mongodb.ts34.d.ts:31:10 - error TS2305: Module '"tls"' has no exported member 'TLSSocketOptions'." Any help for how to resolve this apparent node package issue would be very helpful!
When I inspect, the tls package it has TLSSocketOptions and appears to have it exported through the following at the end of the file
declare module 'node:tls' {
export * from 'tls';
}
My package.json
{
"main": "wwwroot/index.js",
"bin": "wwwroot/index.js",
"scripts": {
"start": "node ./wwwroot/index.js",
"dev": "ts-node src/",
"compile": "npx tsc",
"postinstall": "npx tsc"
},
"engines": {
"node": "14.18.1"
},
"dependencies": {
"#discordjs/uws": "^10.149.0",
"discord.js": "^11.4.2",
"mongodb": "^4.1.4",
"snoowrap": "^1.23.0"
},
"devDependencies": {
"#types/node": "^11.15.54",
"pkg": "^4.5.1",
"ts-node": "^10.4.0",
"tslint": "^5.16.0",
"typescript": "^3.9.10"
}
}
Your version of #types/node seems to be ancient and I guess the export in question was added since then

Cannot download GraphQL schema from endpoint

I'm currently using graphql-cli from Prisma to download the schema from endpoint. But, even after I deploy the changes I made to my schema, which gets deployed successfully, whenever I try to download the schema, I get project prisma - No changes. And the generated prisma.graphql is left unchanged.
I use the following command to download the schema:
graphql get-schema -p prisma --dotenv config/dev.env
dev.env is simply to get PRISMA_ENDPOINT=http://localhost:4466/ environment variable.
I tried to generate prisma.graphql in a different way by having the following in prisma.yml:
endpoint: ${env:PRISMA_ENDPOINT}
datamodel: datamodel.prisma
generate:
- generator: graphql-schema
output: ./generated/
And executed prisma generate, but I get the error:
▸ [WARNING] in
/Users/F/Documents/d/server/prisma/prisma.yml: A valid
environment ▸ variable to satisfy the declaration
'env:PRISMA_ENDPOINT' could not be found.
Tried stopping and recreating Docker as well as deleting the node_module and re-installing, but to no avail.
My package.json:
{
"name": "graphql-basics",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node dist/index.js",
"heroku-postbuild": "babel src --out-dir dist --copy-files",
"dev": "env-cmd ./config/dev.env nodemon src/index.js --ext js,graphql --exec babel-node",
"test": "env-cmd ./config/test.env jest --watch --runInBand",
"get-schema": "graphql get-schema -p prisma --dotenv config/dev.env"
},
"jest": {
"globalSetup": "./tests/jest/globalSetup.js",
"globalTeardown": "./tests/jest/globalTeardown.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"#babel/polyfill": "^7.0.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"bcryptjs": "^2.4.3",
"cross-fetch": "^2.2.2",
"env-cmd": "^8.0.2",
"google-auth-library": "^4.2.3",
"graphql-cli": "^3.0.14",
"graphql-yoga": "^1.14.10",
"jsonwebtoken": "^8.3.0",
"prisma-binding": "^2.1.1"
},
"devDependencies": {
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"jest": "^23.5.0",
"nodemon": "^1.17.5"
},
"resolutions": {
"graphql": "^14.5.8"
}
}
To fix the error "variable to satisfy the declaration 'env:PRISMA_ENDPOINT' could not be found." when calling prisma generate you should either set the PRISMA_ENDPOINT variable manually or load it via dotenv. For example you could run npx dotenv -- prisma generate to load the env vars from your .env file.
To download the schema from the endpoint via graphql get-schema make sure to provide a properly configured .graphqlconfig.yml and provide the correct project.
A sample configuration for a prisma project could look like this:
projects:
prisma:
schemaPath: 'src/schema.graphql'
extensions:
endpoints:
default: 'http://localhost:4000/graphql'
database:
schemaPath: 'src/generated/prisma.graphql'
extensions:
prisma: 'database/prisma.yml'
endpoints:
default: 'http://localhost:4466'

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

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.

TestsNodejs - compilers deprecation in mocha and babel

I'm building api rest on node with integration test (Babel, chai, mocha)
I changed
--compilers js:babel-core/register
for
--require babel-core/register
as recommended by the documentation:
https://github.com/mochajs/mocha/wiki/compilers-deprecation
But when I make this change in my mocha.opts file, the error appears:
C:\Users\Ranulfo\Desktop\noderest>npm run test-integration
> noderest#1.0.0 test-integration C:\Users\Ranulfo\Desktop\noderest
> mocha --opts test/integration/mocha.opts test/integration/*.js
C:\Users\Ranulfo\Desktop\noderest\test\integration\helpers.js:1
(function (exports, require, module, __filename, __dirname) { import supertest f
rom 'supertest';
^^^^^^
SyntaxError: Unexpected token import
For more details of the code:
mocha.opts(test/integration/mocha):
--require test/integration/helpers.js
--reporter spec
--require babel-core/register
--slow 5000
helpers.js (test/integration/helpers.js)
import supertest from 'supertest';
import chai from 'chai';
import app from '../../app';
global.app = app;
global.request = supertest(app);
global.expect = chai.expect;
package.json
{
"name": "noderest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "babel-node ./index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test-integration": "mocha --opts test/integration/mocha.opts test/integration/*.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-node6": "^11.0.0",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"mocha": "^5.2.0",
"supertest": "^3.1.0"
},
"dependencies": {
"express": "^4.16.3",
"sequelize": "^4.37.10",
"sqlite3": "^4.0.0"
}
}
.babelrc
{
"presets": ["env"]
}
For the people who are just here due to the error of deprecation notice of using compilers and have no other issues with the babel.
Just install the dependency #babel/register.
And change the test script from mocha --compilers js:babel-core/register to mocha --require #babel/register
This should solve your error regarding the error notice from mocha.
With the update of "Babel" for version 7, the addition of some configuration steps has occurred.
To adjust the "bug" do the following steps:
Install the specified dependency:
npm install --save-dev # babel / register
Change the "mocha.opts" file to the compilers for the following line:
--compilers js: # babel / register
Once this is done, your workflow should be normalized. (I.e.
Note: Look out for the following dependencies that are not present in the tutorial.
npm install --save-dev #babel/cli
npm install --save-dev #babel/core
npm install --save-dev #babel/node
npm install --save-dev #babel/preset-env
npm install --save-dev #babel/register
Auxiliary content:
Setup: https://babeljs.io/setup#installation
Migration: https://babeljs.io/docs/en/v7-migration
Mocha issues - Compilers deprecation https://github.com/mochajs/mocha/wiki/compilers-deprecation

Resources