Using ts-node with ESM module: ipfs-http-client - commonjs

I am trying to run some tests on files I created which use ipfs-http-client.
I write code with typescript and I am using ts-node to run files without actually doing the build (when i do tests). I am running this code in a commonJs folder. As ipfs packages is ESM only, it gives me an error each time i try to run this code
No "exports" main defined in ..... (points to the ipfs folder in node_modules)
To reproduce the issue I created a bare project which shows exactly what is happening.
I run npm init and npm i ipfs-http-client.
I have already installed ts-node.
the packages.json file looks like this
{
"name": "import_esm_in_commonjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ipfs-http-client": "^59.0.0"
}
}
and, for tsConfig file, I am just using the basic ts-node one:
{
"ts-node": {
"cwd": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS",
"projectSearchDir": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS"
},
"compilerOptions": {
"lib": [
"es2021"
],
"module": "commonjs",
"target": "es2021",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": [
"node"
],
"sourceMap": true,
"inlineSourceMap": false,
"inlineSources": true,
"declaration": false,
"noEmit": false,
"outDir": "./.ts-node"
}
}
I just created an index.ts file in the root of the project which looks like this:
import "ipfs-http-client"
console.log("file works");
when I run ts-node index.ts I receive the previously mentioned error:
No "exports" main defined in ..... (pointing to the ipfs folder in the node_modules)
Hope someone can help!

Actually I found how to do it.
In order to be able to import esm only modules in a typescripy project with "type": "commonJs"in the package.json like this:
import { create } from "ipfs-http-client";
console.log(create);
I did the following:
explicitly set tsconfig file for ts-node:
{
"ts-node": {
"cwd": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS",
"projectSearchDir": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS"
},
"compilerOptions": {
"lib": [
"es2021"
],
"module": "commonjs",
"target": "es2021",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": [
"node"
],
"sourceMap": true,
"inlineSourceMap": false,
"inlineSources": true,
"declaration": false,
"noEmit": false,
"outDir": "./.ts-node"
}
}
set "module": "ES2022" and "modeResolution": "Node" in the tsconfig.json in the tsconfig.json
{
...
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "Node",
...
}
when calling ts-node use the --esm flag:
ts-node --esm name_of_the_file
and it works =)
ps: I also updated the repo

Related

Dependencies aren't being included on the build output of electron forge

My electron app project uses create-react-app and my main process needed the electron-devtools-installer module. I'm using electron-forge for building the app. Now after running npm run make to package and built the app, I'm getting this error message
Error: Cannot find module 'electron-devtools-installer'
after opening the app. I'm not getting this error when I simply run electron ., thus I suspect the module is not being included on the packaging.
Now the said module is for development only, I could simply write a logic to ignore it when in production mode but what happens when I really need a 3rd party modules in my main process in the production, so how do I include the dependencies modules in the packaging?
I moved the electron-devtools-installer mode in dependencies key in package.json but it still doesn't work.
My package.json contains:
{
"name": "myapp",
"version": "0.1.0",
"private": true,
"homepage": ".",
"description": "some description",
"main": "main/init.js",
"author": "anonymouse",
"license": "MIT",
"dependencies": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"electron-devtools-installer": "^3.2.0",
"electron-squirrel-startup": "^1.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "electron-forge start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "concurrently -k \"react-scripts start\" \"npm:electron\"",
"electron": "wait-on tcp:3000 && electron .",
"package": "react-scripts build && electron-forge package",
"make": "react-scripts build && electron-forge make",
"clean:winbuild": "rmdir build /s /q & rmdir out /s /q"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#electron-forge/cli": "^6.0.0-beta.67",
"#electron-forge/maker-deb": "^6.0.0-beta.67",
"#electron-forge/maker-rpm": "^6.0.0-beta.67",
"#electron-forge/maker-squirrel": "^6.0.0-beta.67",
"#electron-forge/maker-zip": "^6.0.0-beta.67",
"concurrently": "^7.4.0",
"dotenv": "^16.0.3",
"electron": "^21.1.1",
"eslint": "^8.25.0",
"eslint-config-google": "^0.14.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.3.0",
"eslint-plugin-promise": "^6.1.0",
"eslint-plugin-react": "^7.31.10",
"wait-on": "^6.0.1"
},
"config": {
"forge": "./forge.config.js"
}
}
My forge.config.js:
module.exports = {
packagerConfig: {
ignore: [
/coverage.*/,
/node_modules.*/,
/public.*/,
/src.*/,
/\.env|\.gitignore|.*bak|eslint.*|package-lock.*|(?<!build.*)\.txt|(?<!build.*)\.md/
]
},
makers: [
{
name: '#electron-forge/maker-squirrel',
config: {
name: 'bkkgapp'
}
},
{
name: '#electron-forge/maker-zip'
}
]
}

Typings/autocomplete suggestions for custom library not coming in Expo projects

I made a library with Typescript and React Native and published it on npm, but when I'm using it in any expo project I'm not getting any typings or autocomplete suggestions. Can anyone tell me the steps to solve this issue? Also, I've used Callstack react-native-builder-bob for the setup of this library.
My tsconfig right now is :
{
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"paths": {
"my-library": ["./src/index"]
},
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"esModuleInterop": true,
"importsNotUsedAsValues": "error",
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"lib": ["esnext", "dom"],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noImplicitUseStrict": false,
"noStrictGenericChecks": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext"
}
}

How to use vue development mode with Django

I don't know how to run development mode with Django. I'm running webpack and when I finish all the Vuejs work I just bundle all to a folder where Django serves It as a static file. I know taht I've to run webpack with mode development but that doens't work, It gives me a cannot found error.
I'd like to run Vuejs in development mode alongside with Django, how can I do that?
I'll share to you my project structure, my package.json and webpack config.
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
entry: './frontend/Vue/main.js',
output: {
filename: 'build.js',
path: path.resolve(__dirname, './static/js'),
publicPath: '/static/js'
},
module: {
rules: [
{
test: /\.(js)$/,
exclude: /(node_modules)/,
use: [
{ loader: 'babel-loader' }
]
},
{
test: /\.(css|scss)/,
use: [
'style-loader'
]
},
{
test: /\.vue$/,
exclude: /node_modules/,
loader: 'vue-loader'
}
]
},
devServer: {
// contentBase: ponerle la ruta del index de django
contentBase: path.join(__dirname, 'templates'),
watchContentBase: true,
historyApiFallback: true,
noInfo: true
},
plugins: [
new VueLoaderPlugin()
]
}
package.json
{
"name": "TuDistribuidora",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server --mode development --port 9000 --open",
"build": "webpack --progress"
},
"repository": {
"type": "git",
"url": "git+https://github.com/EmiBuffet/TuDistribuidora.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/EmiBuffet/TuDistribuidora/issues"
},
"homepage": "https://github.com/EmiBuffet/TuDistribuidora#readme",
"dependencies": {
"vue": "^2.6.11",
"vue-router": "^3.4.5",
"vuex": "^3.5.1"
},
"devDependencies": {
"#babel/core": "^7.11.1",
"babel-loader": "^8.1.0",
"babel-preset-es2015": "^6.24.1",
"node-sass": "^4.14.1",
"sass-loader": "^9.0.3",
"style-loader": "^1.2.1",
"vue-loader": "^15.9.3",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0",
"bulma": "^0.9.1"
}
}
project structure:
Using django-webpack-loader in your Django project in combination with webpack-bundle-tracker in your webpack build will provide the connection needed to alternate between production/dev Vue builds quickly. The bundle information will be recorded by webpack into a webpack-stats.json which will then be used by Django to fetch/load the correct bundles for a given view.
I have an article explaining step-by-step how to set up a project with the above techniques.
Good hacking!

Unit testing in browser for NativeScript fails because of .tns. files

I have an Angular flavoured Nativescript project, which must be tested with "vanila" Jasmine, in a browser (so not in mobile) with ng test.
By default, with "naked" tests, it works. But the problem is, if I try to test/import anything, that has a ".tns" alternative, in some cases it loads that, and the build fails.
My problem is similar to this thread but there were no good solution described there.
So for instance:
I have two files:
app.component.tns.ts
app.component.ts
and I try to import it for testing in app.component.spec.ts:
import {AppComponent} from "#src/app/app.component";
it loads the .tns. file, and the build fails, as it cannot load the mobile-specific libraries.
ERROR in ./src/app/app.component.tns.ts
Module not found: Error: Can't resolve 'nativescript-ui-sidedrawer' in '/home/..../src/app'
resolve 'nativescript-ui-sidedrawer' in '/home/...../src/app'
Parsed request is a module
using description file: /home/...../src/package.json (relative path: ./app)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
...
# ./src/app/app.component.tns.ts 25:35-72
# ./src/app/app.module.spec.ts
# ./src sync \.spec\.ts$
# ./src/test.ts
is there any solution to "remove" the .tns. files, just as if I were running a simple ng serve?
update: My tsconfig.spec.json should exclude these files, but it does not work either ...
"exclude": [
"**/*.tns.ts",
"**/*.android.ts",
"**/*.ios.ts"
]
}
it seems the problem was with tsconfig.json. Specificly this part:
"compilerOptions": {
...
"paths": {
"#src/*": [
"src/*.android.ts",
"src/*.ios.ts",
"src/*.tns.ts",
"src/*.web.ts",
"src/*.ts"
]
},
As this was extended by the tsconfig.spec.json.
I modified the tsconfig.spec.json to this:
{
"compilerOptions": {
"target": "es5",
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2017",
"dom",
"es6",
"es2015.iterable"
],
"baseUrl": ".",
"resolveJsonModule": true,
"esModuleInterop": true,
"paths": {
"#src/*": [
"src/*.ts"
]
},
"outDir": "../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"src/test.ts",
"src/polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
],
"exclude": [
"**/*.tns.ts",
"**/*.android.ts",
"**/*.ios.ts"
]
}
and now the tests run, and the correct components are imported.

Command line way to generate HTML coverageReport in Jest

In Jest, is there a way from the command line to generate an HTML coverage report if it is not defined in the jest.config.js files?
I only want to generate the HTML report some of the time rather than every time I run Jest. The only way I've been able to generate the HTML report was by changing the config manually.
By default the collectCoverage option for jest is set to false. The easiest way to get an HTML coverage report is to configure jest in either package.json or jest.config.js. You will also need to set the coverageDirectory in the config as well.
There are several different configuration options for coverage reporting. See here for all config settings: https://jestjs.io/docs/en/configuration.html#collectcoverage-boolean
Here is an example of how to configure jest in package.json with a few options.
{
"name": "appname",
"version": "1.0.0",
"description": "description",
"main": "index.js",
"scripts": {
"test": "jest",
"postinstall": "jspm install"
},
"jest": {
"scriptPreprocessor": "./preprocessor.js",
"testPathIgnorePatterns": [
"/node_modules/",
],
"unmockedModulePathPatterns": [
"./node_modules/react"
],
"collectCoverage": true,
"coverageDirectory": "path/to/coverage/reports",
},
"author": "author",
"license": "ISC",
"dependencies": {
"del": "^1.1.1",
"gulp": "^3.8.11",
"gulp-filter": "^2.0.2",
"gulp-load-plugins": "^0.10.0",
"gulp-react": "^3.0.1",
"gulp-shell": "^0.4.1",
"harmonize": "^1.4.1",
"jest-cli": "^0.4.1",
"jspm": "^0.15.5",
"react": "^0.13.2",
"react-tools": "^0.13.2",
"run-sequence": "^1.1.0"
},
"devDependencies": {
"browser-sync": "^2.7.1",
"gulp": "^3.8.11"
},
"jspm": {
"directories": {},
"dependencies": {
"react": "npm:react#^0.13.2"
},
"devDependencies": {
"babel": "npm:babel-core#^5.1.13",
"babel-runtime": "npm:babel-runtime#^5.1.13",
"core-js": "npm:core-js#^0.9.4"
}
}
}
Now when you run your test with jest --coverage the HTML reports will be generated in the specified directory.
Have you tried to pass in a JSON config via the command line?
npx jest ./src/**/*.js --coverage --config='{ "coverageReporters": ["html"] }'
Here is a portion from my test scripts in package.json that may help you generate coverage reports:
"test": "react-scripts test --env=jsdom",
"test:coverage": "react-scripts test --coverage"
My setup uses jest#23.6.0 with enzyme#3.8.0 and jest-enzyme#7.0.1