typescript - What's wrong here - node.js

I'm new to TypeScript and I'm trying to import the fs package from node.js, however this syntax is wrong and I'm not sure what am I supposed to do.
import * as "fs" from "fs";

What import does is it puts imported expressions into identifiers - or JavaScript variables. You need the syntax for an identifier to put the fs namespace into:
import * as fs from "fs";
Doing as "fs" doesn't work because what follows the as must be the variable name to put the namespace into, not a string.

Related

Create own class

created a new project in expressjs. I want to create my own class. So I created a file MyClass.js in /routes/.
class MyClass {
constructor() {
}
}
export default MyClass;
And in /routes/index.js i added:
import MyClass from './MyClass';
And I have an error:
import MyClass from './MyClass;
^^^^^^
SyntaxError: Cannot use import statement outside a module
[...]
What I'm doing wrong?
Your project is not set up properly to interpret the file that has the error as an ESM module file. It is, instead, being interpreted as a CommonJS module (where you use require(), not import) which is the nodejs default.
There are a number of ways to tell nodejs that your file is supposed to be an ESM module.
You can give it a file extension of .mjs.
You can add "type": "module" to your package.json file.
You can use the --input-type=module command line argument when starting node.js if this file is your top level file you're executing.
See nodejs doc here on this subject.
FYI, you could just switch to the CommonJS syntax of using require() and module.exports instead of import and export, but I assume what you really want to do is to tell nodejs that you want to use ESM modules so you can use the more modern import and export syntax.
Well first of all to export your class you suppose to use exports or module.export
and to import it you suppose to use require('') not import.
Those features are only available with .mjs or if you will setup babel though soome es6 features are yet to land in

Cannot use import statement outside a module when importing "uuid" [duplicate]

This question already has answers here:
SyntaxError: Cannot use import statement outside a module
(34 answers)
Closed 1 year ago.
trying to import 'uuid' in the user model as
import { v1 as uuidv1 } from "uuid";
But, I don't know why it gives syntaxError like this
import { v1 as uuidv1 } from "uuid";
^^^^^^SyntaxError: Cannot use import statement outside a module
-I am assuming that you have used export.modules in your uuid.js.
Also i am assuming that uuid.js file is present in the same directory as the one in which your are importing the function.
Now if you want to use the functions present in uuid.js to another file then you can use require() to import the function in your other js file. Try using below syntax to import the function.
const v1=require(./uuid);
Now you can use v1.uuidv1(); in your js file.
However if you want to specifically use the syntax which you have used then you can refer the link which have been provided by Daniel A White

python 3, can't understand import system

this is such a simple problem but I cant seem to find any direct explanation to this.
in module.py
def foo():
print("foo")
in main.py
import module
foo()
it will result in an error saying that foo is not defined? when i look for the answer online, I can't find anything surprisingly
I'm not planning to use things like
from x import y
just straight up the import system
When you import an external module, it generates a variable named module that contains all classes, functions and variables from the module. To acess 'foo' function you need to first acess the module:
module.foo()
To import 'foo' function you can import everything from the module, like this:
from module import *
Now you can simply do: foo()
You can also set a custom name to the module, like:
import module as M
And now you can run 'foo' like this:
M.foo()
PS: I'm not english native
The statement
import module
makes the name of module module available. So you can use module.foo().
If you want to call foo() without "qualifying" it:
from module import foo
or
from module import *
but that latter is bad idea because you are liable to import unexpected names, which may collide with other names you imported from other modules.
from model import foo
is the preferred way as in any kinds of
from model import *
you (and anyone ever working on that code) has no idea what has been imported. Could even lead to name conflicts.

Why use import and require() in Node.js code?

I am reading the source code at "Type definitions for Express 4.16" and found this funny line (#18):
import serveStatic = require("serve-static");
Since import is the new way to work with modules in ES6, why above code is used or needed at all?
Type definitions for Express 4.16 is written(index.d.ts) in typescript, Where import = require() is a TypeScript Syntax
TypeScript - Modules (export = and import = require())
The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
Reference : Modules

fs.readFileSync is not a function

I am using rn-nodeify for enabling the use of pdf2json in React Native. pdf2json uses fs for loading files using the method readFileSync. I am getting this error when I try to use the library:
fs.readFileSync is not a function. (In 'fs.readFileSync(_basePath + fieldName, 'utf8')', 'fs.readFileSync' is undefined)
I cannot find any support for this issue. Any pointers appreciated.
Edit: Please note that I am not trying to run this in a browser. This pertains to react-native i.e. it runs on a device, and the code should have access to the file system.
FWIW, I came across this problem while writing tests in ES6/latest JS - fixed this by changing the import from:
import { fs } from 'fs';
to
import fs from 'fs';
Notice the unnecessary {} - fs is the default export from the fs node module and should be imported in the latter way
For a better explanation on default & named exports please see this SO discussion

Resources