Ratan Uday Kumar
Ratan Uday Kumar

Reputation: 6512

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

Development using nodejs for running (--experimental-modules)

Current visual studio code intelligence import as below

import config from "./config";

but required as below

import config from "./config.js";

Without .js getting error as below

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??

//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;

Upvotes: 11

Views: 6631

Answers (2)

Waseem
Waseem

Reputation: 761

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 (i.e via intellisense when referencing an export of another file and VSCode imports it automatically). It does not work with with autosuggest when typing the import statement manually.

Upvotes: 23

Suman Kumar Dash
Suman Kumar Dash

Reputation: 704

I always used my config.js like this this. 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

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;

Upvotes: -1

Related Questions