Import dependency with bower Ember CLI - could not find module - node.js

I'm trying to use Markdown-it in Ember Helper. First I installed it with Bower and tried to import it.
app.import('bower_components/markdown-it/dist/markdown-it.js');
In helper:
import MarkdownIt from "markdown-it";
This is showing error Could not find module: markdown-it. Then I tried to use Ember-browserify and install Markdown-it via npm. I tried to import it in helper
import MarkdownIt from "npm:markdown-it";
export default Ember.Handlebars.makeBoundHelper(function(input){
var result = MarkdownIt.render(input);
return new Ember.Handlebars.SafeString(result);
});
This is showing error TypeError: a.default.render is not a function.
I also tried
import MarkdownIt from "npm:markdown-it";
export default Ember.Handlebars.makeBoundHelper(function(input){
var md = new MarkdownIt();
var result = md.render(input);
return new Ember.Handlebars.SafeString(result);
});
This is showing Error: Could not find module npm:markdown-it imported from my-new-app/helpers/format-markdown

The library you're trying to use doesn't provide a name for itself when using AMD, so there's no way to import it via a name. See https://github.com/ember-cli/ember-cli/issues/770 for more information about this.
It does look like "markdown-it" also exposes itself as a global, so you can always access it that way:

Related

Module not found: Error: Can't resolve 'fs' when trying to use a NodeJS library

I initiated a basic ReactJS app using npx create-react-app, then I ejected using npm run eject. Now when I am trying to import the Casual library by import casual from 'casual';, I get the following error:
Compiled with problems:
ERROR in ./node_modules/casual/src/casual.js 3:13-37
Module not found: Error: Can't resolve 'fs'
in '/home/me/project/node_modules/casual/src'
And the code around line number 3 in casual.js looks like this:
var helpers = require('./helpers');
var exists = require('fs').existsSync;
var safe_require = function(filename) {
if (exists(filename + '.js')) {
return require(filename);
}
return {};
};
...
I found answers to similar questions. Those were mainly Node or Angular related. I also tried answers suggesting some changes in webpack config, but no luck.
The reason is Casual doesn't work on the front end. It runs on Node.js only.
You need to install maybe a new package to make things work.
Fs is unavailable on the browser so it won't work. Instead, you should use casual-browserify, it will work on browsers.

SyntaxError: Cannot use import statement outside a module when importing axios

In my node.js (express.js) project, I try to wrap axios to create a HttpClient like this:
import axios from 'axios';
const httpClient = axios.create({ baseURL: 'http://locahost:3003' });
...
export const httpClient = {
...
};
When I run it, I get error SyntaxError: Cannot use import statement outside a module which complains the line import axios from 'axios'. How can I get rid of this error? (I come from React-native world, I used the same thing in React-Native, it is fine)
The issue is by default express uses CJS format of importing as you might have seen const axios = require("axios").
What you are trying to do is an ESM format of import which is not understood by the JS as natively. To work import statements you can either add the following property to your package.json file - "type": "module" or you can use ".mjs" extension instead of default ".js" extension.
Hope this link helps further.

Jest tests - Please add 'import "reflect-metadata"' to the top of your entry point

I'm developing an application using dependency injection with tsyringe. That's the example of a service that receives the repository as a dependency:
import { injectable, inject } from 'tsyringe'
import IAuthorsRepository from '#domains/authors/interfaces/IAuthorsRepository'
#injectable()
export default class ListAuthorsService {
constructor (
#inject('AuthorsRepository')
private authorsRepository: IAuthorsRepository
) {}
And the dependencies container:
import { container } from 'tsyringe'
import IAuthorsRepository from '#domains/authors/interfaces/IAuthorsRepository'
import AuthorsRepository from '#domains/authors/infra/typeorm/repositories/AuthorsRepository'
container.registerSingleton<IAuthorsRepository>(
'AuthorsRepository',
AuthorsRepository
)
export default container
In the tests, I don't want to use the dependencies registered on the container, but instead, to pass a mock instance via parameter.
let authorsRepository: AuthorsRepositoryMock
let listAuthorsService: ListAuthorsService
describe('List Authors', () => {
beforeEach(() => {
authorsRepository = new AuthorsRepositoryMock()
listAuthorsService = new ListAuthorsService(authorsRepository)
})
But I'm receiving the following error:
tsyringe requires a reflect polyfill. Please add 'import
"reflect-metadata"' to the top of your entry point.
What I thought was - "I may need to import the reflect-metadata package before executing the tests". So I created a jest.setup.ts which imports the reflect-metadata package. But another error occurs:
The instance of the repository is somehow undefined.
I would like to run my tests in peace.
First create in root of your project an jest.setup.ts.
In your jest.config.js, search for this line:
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
uncomment, and add your jest.setup.ts file path.
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
Now import the reflect-metadata in jest.setup.ts:
import 'reflect-metadata';
And run tests again.
I went through the same problem here and refactoring the test find out that it has to import the dependencies first and then the class of service that will be tested

Why is Rollup plugin loading correctly via local file but not using NPM library?

I have a very simple Rollup plugin I made. This works great if I run with the following import (using local relative)...
import { SassShadow } from '../../../rollup-sass-shadow/index.mjs';
But when I try to run with the npm version like this...
import { SassShadow } from '#jrg/rollup-sass-shadow';
I get...
[!] TypeError: defaultLoader is not a function
TypeError: defaultLoader is not a function
How do I get this to work?
The only way I could get this to work was to use the default export instead of the named export. So instead of...
export class SassShadow{...}
import { SassShadow } from '#jrg/rollup-sass-shadow';
It needed to be...
class SassShadow{...}
export default SassShadow
import SassShadow from '#jrg/rollup-sass-shadow'

Node.js - ES6 module import to Node.js 'require'

What is the Node.js 'require' equivalent to the following ES6 import?
import RNFetchBlob from 'react-native-fetch-blob'
Thank you
There's no default export in require so you don't have an exact equivalent.
The practice with node modules when you have only one value to export is to make it the module exports:
module.exports = ...
In such a case, you'll be able to import using
var RNFetchBlob = require('react-native-fetch-blob');
Solution
I got it working with:
var RNFetchBlob = require('react-native-fetch-blob').default;
For more details, check out this.

Resources