简体   繁体   English

使用typescript在Node js中导入类

[英]importing class in Node js with typescript

I would import class in nodejs and use it in app.ts 我会导入类和的NodeJS在使用它app.ts

var nano = require("nano");
import { EnvConfig } from './envConfig.service';
let config = new EnvConfig();
const dbCredentials: any = config.appEnv.getServiceCreds('dataservices');
export const nanodb = nano({
  url: dbCredentials.url,
});
export const nanodbCockpitLight = nanodb.use('data');
console.log(dbCredentials);

When I try to compile I get this error. 当我尝试编译时,出现此错误。

import { EnvConfig } from './envConfig.service';
       ^
SyntaxError: Unexpected token {

I have created the tsconfig file : 我已经创建了tsconfig文件:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "allowJs": true,
    "outDir": "./dist",
    //"baseUrl": "src" // Attention !! nécessite l'utilisation d'un loader de module node pour fonctionner sur node
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

I get this warning 我得到这个警告

No inputs were found in config file 'c:/Users/EHHD05911.COMMUN/Documents/cockpitLight/DB mananger/tsconfig.json'. 在配置文件'c:/Users/EHHD05911.COMMUN/Documents/cockpitLight/DB mananger / tsconfig.json'中找不到输入。 Specified 'include' paths were '["src/ / "]' and 'exclude' paths were '["node_modules"," / .spec.ts"]' 指定的“包含”路径为“ [[src / / “]”,而“排除”路径为“ [” node_modules“,” / .spec.ts“]”

您无法直接运行无法正常运行的节点app.ts文件。您需要像babel js或typescript编译器tsc这样的编译器,因此请先转换为js文件,然后再运行节点app.js

You're using .js extension, you need .ts extension, eg: app.ts instead of app.js . 您正在使用.js扩展,你需要.ts扩展名,例如: app.ts代替app.js

Make sure you have typescript either in npm global or in dev dependencies. 确保在npm global或dev依赖项中都有打字稿。

I suspect whatever you're importing has typescript syntax (strong typing and such), and so running node directly won't work. 我怀疑您要导入的任何内容都有打字稿语法(强类型打字等),因此直接运行node将无法工作。 You need to run tsc first, which will transpile everything to javascript in a dist folder, and then run node dist/app.js . 您需要先运行tsc ,它将所有内容转换为dist文件夹中的javascript,然后运行node dist/app.js

This is a bit cumbersome though, which is why there is ts-node . 不过这有点麻烦,这就是为什么有ts-node的原因 It's exactly what it sounds like, a node REPL for typescript. 听起来确实像是打字稿的节点REPL。 You should be able to run ts-node src/app.ts . 您应该能够运行ts-node src/app.ts

import { something } is a typescript syntax, it won't work in a .js file. import { something }是一种打字稿语法,在.js文件中不起作用。 That is a separate language. 那是另一种语言。 Try using require instead. 尝试改用require

Use babel js which is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. 使用babel js,这是一个工具链,主要用于在当前和较旧的浏览器或环境中将ECMAScript 2015+代码转换为JavaScript的向后兼容版本。

package.json package.json

"dependencies": {
"@babel/polyfill": "^7.0.0",
}

"babel": {
"presets": [
  "@babel/preset-env"
]

}, },

"scripts": {
    "start": "server.js --exec babel-node",
}

https://babeljs.io/docs https://babeljs.io/docs

This will enable/resolve your import statements. 这将启用/解析您的导入语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM