what is a import satement in nodejs instead of require - node.js

I was going through this module, and found many of the files using the import stament.ex: this one.
can anybody tell me how the import statement is replacing the standerd require statement of nodejs and how are they working?
EDIT:
this is not the duplicate, because the import syntax is different form the ES6 syntax

The import statement is provided by js.io - A module system that the repository is using.
Quoting from the README of the project:
js.io is a multi-platform package management and module system for
JavaScript. js.io modules can be evaluated in a JavaScript runtime
(e.g. node.js) or precompiled into a single package for use on the
client side.
js.io provides the following:
A module system. Dependency graph that works in the client and the
browser. Support and networking libraries that can be used on either
platform.
The import statement as in the linked example does not confirm to ES6 spec.
From MDN, the syntax for ES6 imports follow the following patterns:
import name from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as alias from "module-name";
import defaultMember from "module-name";
import "module-name";
The usage import AudioManager as exports; is not a valid usage as per the above rules.
I could not deduce from the README of js.io if confirmance with ES6 modules is a goal of the project.

Related

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

Import current module by name in typescript

Is it possible for a module to import "itself" by its own name in typescript?
For instance, lets say there is a module my-module with a bunch of test.
Is there a chance to import it inside the tests using import ... from "my-module" instead of using local path (e.g. import ... from "./my-module")?
As I know it can be done using the require, but it seems that import does not support this.
You can use the TypeScript tsconfig paths option e.g.
{
"paths": {
"module": ["src/path/to/module"]
}
}
More
https://www.typescriptlang.org/docs/handbook/module-resolution.html

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_modules import from a directory inside what is specified in "main" (package.json)?

Is it possible to import from something other that what "main" points to?
In my library that is installed into node_modules, I have the main set to
lib/index.js
so (using es2015 imports - source was ts compiled js), I can do
import { FunctionA, FunctionB } from 'MyTestLibrary';
This works because these functions are exported inside of index.js under libs.
I also have an index inside a directory which exports functionC and functionD, the structure is here
/lib/otherdir/index.js
so if I do an import like so
import { FunctionC, FunctionD } from 'MyTestLibrary/otherdir';
my IDE does not complain but running the application I get a
Cannot find module MyTestLibrary/otherdir
Everything is exported as it should be.
You can access the directory directly like this:
import { FunctionC, FunctionD } from 'MyTestLibrary/lib/otherdir'

Creating node module with subfolders

I am developing npm module. I have following index.ts
export * from './src/A/index';
At this moment, in app import statement looks as follow:
import {something} from 'myModule';
I would like to add some extra logic to my module, and organize it like:
export * from './src/A/index';
export * from './src/B/index';
export * from './src/C/index';
What should I write in index.ts to make my module to be used in this way:
import {something} from 'myModule/A'
import {something2} from 'myModule/B'
etc.?
What should I write in index.ts to make my module to be used in this way
Given that you want to use import {something} from 'myModule/A' implies that myModule/A folder should have an index.ts that exports something.
This conflicts with the fact that A/index is actually located in myModule/src/A/index.
Move the file and it should work.

Resources