Modules exported using es6 syntax for the browser - ecmascript-6

I have a module module-A which has the following in index.js
import SomeModule from './lib/some-module'
import AnotherModule from './lib/another-module'
module.exports ={
SomeModule: SomeModule,
AnotherModule: AnotherModule
}
I then use this module-A from another module-B:
import SomeModule from `module-A`
I now want to build module-B for the browser, so I use browserify from the module-B cwd: [I am using babel 6.0]
browserify index.js -t [ babelify --presets [ es2015 ] ]
This throws an error as
import SomeModule from './lib/some-module'
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
Is there anyway I can export modules using es6 export and import syntax and yet build properly for the browser.

Related

pm2 can't start typescript ESM

I want to start typescript ESM in pm2
I configure ESM module:
add "type": "module" to package.json
add "module": "Node16" to tsconfig.json
add "esm": true to ts-node section of tsconfig.json
I try to start a simple program main.ts using pm2:
import { msg } from './lib/helper.js';
msg('Hello world!');
and received this error:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /var/node/srv2/src/main.ts
standard command work ok: ts-node src/main.ts
I try to use ecosystem.config.cjs - but it's not work too.
I put full example to github:
https://github.com/gayratv/pm2-typesript-esm
Is any way to start typescript ESM using pm2 ?

Cannot find module '../test/tests.json'. Consider using '--resolveJsonModule' to import module with '.json' extension

I have an error when trying to import a json file into a typescript file.
My tsconfig.json file contains "esModuleInterop": true, "resolveJsonModule": true.
I have tried both
import * as uiObject from '../test/tests.json';
and
import uiObject from '../test/tests.json';

Module not found: Error: Can't resolve 'angular-signature-pad'

I got below error, try adding signature to my angular 6 project
node module : - angular-signature-pad
error: - ERROR in src/app/app.module.ts(5,41): error TS2307: Cannot find module 'angular-signature-pad'.
Things to check?
1) Did you install the package ?
npm install angular-signature-pad --save
2) Import the package in app module.
import { AngularSignaturePadModule } from 'angular-signature-pad';
#NgModule({
imports: [
...
AngularSignaturePadModule.forRoot()
]
...
})
export class AppModule { }
Maybe possibility, It may not be working in angular 4 plus.
https://github.com/BioPhoton/angular-signature-pad/issues/2
try updating node.js to version 16+
or
update angular cli
ng update #angular/cli #angular/core

babel-node es6 "Modules aren't supported in the REPL"

babel-preset-es2015 is installed, and is OK with es6 feature just like below let a = 2;.
But can not work with es6 modules feature import fs from 'fs' as following:
$ babel-node --presets es2015
> let a = 2;
'use strict'
> a
2
> import fs from 'fs';
SyntaxError: repl: Modules aren't supported in the REPL
import fs from 'fs';
at File.buildCodeFrameError (/usr/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/index.js:407:15)
at NodePath.buildCodeFrameError (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/index.js:149:26)
at PluginPass.ModuleDeclaration (/usr/lib/node_modules/babel-cli/lib/_babel-node.js:78:20)
at newFn (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/visitors.js:262:19)
at NodePath._call (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:63:18)
at NodePath.call (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:47:17)
at NodePath.visit (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:93:12)
at TraversalContext.visitQueue (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:152:16)
at TraversalContext.visitMultiple (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:110:17)
at TraversalContext.visit (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:182:19)
So what's wrong?
Thanks!
The error message is exactly what it says. You cannot use ES6 module syntax in the REPL, it is unsupported. You can create a small adapter that imports as ES6 and exports as CommonJS:
# es6-to-common.js
import MyThing from './somewhere';
module.exports = MyThing;
Now inside your usual babel-node prompt:
> MyThing = require('./es6-to-common')
Got it from the official doc:http://babeljs.io/docs/usage/cli/
ES6-style module-loading may not function as expected
Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.

Why "gulp-jest" is failing with: "Please run node with the --harmony flag!"?

I get the following error when running gulp test:
Error: Please run node with the --harmony flag!
Here is my gulp task:
var jest = require('gulp-jest');
gulp.task('test', function() {
return gulp.src('src/**/__tests__').pipe(jest({
rootDir: 'src',
scriptPreprocessor: '../node_modules/6to5-jest',
unmockedModulePathPatterns: [ 'react' ]
}));
});
To reproduce: https://github.com/SEEK-Jobs/react-playground
Note that npm test works properly (test is failing as expected), but gulp test fails with the error above. Both npm test and gulp test have the same config object:
{
rootDir: 'src',
scriptPreprocessor: '../node_modules/6to5-jest',
unmockedModulePathPatterns: [ 'react' ]
}
What am I doing wrong?
As far as I can tell the error is thrown from jest-cli.
The reason you don't get it with npm test is that you probably configured npm to run the jest bin script directly, which uses harmonize to programmatically set the --harmony flag.
You can fix by installing harmonize and putting require('harmonize')() in your gulpfile.
It was a bug in gulp-eslint, and it is fixed now in version 0.3.0.