简体   繁体   English

如何在 Visual Studio 代码中导入扩展名和文件?

[英]How to import extension along with files in visual studio code?

Development using nodejs for running (--experimental-modules)使用 nodejs 开发运行(--experimental-modules)

Current visual studio code intelligence import as below当前visual studio代码智能导入如下

import config from "./config";从“./config”导入配置;

but required as below但要求如下

import config from "./config.js";从“./config.js”导入配置;

Without .js getting error as below没有 .js 得到如下错误

internal/modules/esm/resolve.js:61
  let url = moduleWrapResolve(specifier, parentURL);
            ^

Error: Cannot find module C:\Uday\Projects\practice-server\config imported from C:\Uday\Projects\practice-server\index.js
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:61:13)
    at Loader.resolve (internal/modules/esm/loader.js:85:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:191:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
    at link (internal/modules/esm/module_job.js:41:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

So i need visual studio code intelligence for importing with extension??所以我需要 Visual Studio 代码智能来导入扩展??

//index.js
import express from "express";
import config from "./config.js";



const api_app = express();
const api_port = config.api_port
api_app.listen(api_port, () => {
    console.log(`Practice Server started on ${api_port}`);
});

//package.json
{
  "name": "practice-server",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

//config.js
let config = function () { };

config.api_port = 6000;


export default config;

In the global settings (or the project settings), add the following configuration:在全局设置(或项目设置)中,添加如下配置:

  // Preferred path ending for auto imports.
  //  - auto: Use project settings to select a default.
  //  - minimal: Shorten `./component/index.js` to `./component`.
  //  - index: Shorten `./component/index.js` to `./component/index`
  //  - js: Do not shorten path endings; include the `.js` extension.

  "javascript.preferences.importModuleSpecifierEnding": "js",

Note that at the moment this only works for auto imports (ie via intellisense when referencing an export of another file and VSCode imports it automatically).请注意,目前这仅适用于自动导入(即在引用另一个文件的导出时通过智能感知并且 VSCode 自动导入它)。 It does not work with with autosuggest when typing the import statement manually.手动输入导入语句时,它不适用于 autosuggest。

I always used my config.js like this this.我总是像这样使用我的 config.js。 May be it can help you.也许它可以帮助你。

const config = require('./config');

//Now access value from config //现在从配置访问值

const sys_dbconfig = config_data['sys_database'];
const user = configdata['system_admin_name'];

Here is my config.js这是我的 config.js

var config = {
"sys_database": {
    "user": 'postgres',
    "host": 'localhost',
    "database": 'postgres',
    "password": 'postgres',
    "port": "5432"
},
"system_admin_name": "system",
"url":"http://xxx.xx.x.xxx:3000/wscalc1?wsdl"
}

module.exports = config;

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

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