Typescript : require statement not part of an import statement
Asked Answered
P

4

78

Typescript version 2.2.2

I wrote this require in my UserRoutzr.ts

const users = <IUser[]> require(path.join(process.cwd() + "/data"));

TSLint is raising the following warning:

require statement not part of an import statement

if I changed it to :

import users = <IUser[]> require(path.join(process.cwd() + "/data"));

Then it's raising an error :

TS1003 Identifier expected

How should I rewrite this require ? thanks for feedback

Prostatitis answered 30/3, 2017 at 8:39 Comment(2)
Do you need to build the path like that? cannot you just use "./data"? Assuming process is the running process.Sharpnosed
I tried , but ./data will not be defined in the build directory... ?Prostatitis
S
73

TypeScript modules are an implementation of ES6 modules. ES6 modules are static. Your issue comes from the dynamic path: path.join(process.cwd() + "/data"). The compiler can't determine which module it is at compile time, and the linter doesn't like the causes that lead to any.

You should use a static path to the module. At compile time, TypeScript resolves it. And it affects the right exported type (IUser[]) to users.

import users = require("./yourModuleThatExportsUsers");

Notice: If your module data contains just data, you could consider to change it to a JSON file, which could be loaded (Node.js) or bundled (Webpack).

UPDATE (from May 2019) — It is also possible to use dynamic import, with which TypeScript accepts static and dynamic paths:

const users = await import("./yourModuleThatExportsUsers");

See also: TypeScript 2.4 Release Notes

Salesclerk answered 30/3, 2017 at 8:49 Comment(0)
P
5

if your can run the code correctly but eslint report an error, you can add /* eslint-disable */above you error code like this


/* eslint-disable */
const path =  require("path");
module.exports = {
    lintOnSave: false,
    chainWebpack: config => {

        const dir = path.resolve(__dirname, "src/assets/icons");

        config.module
            .rule("svg-sprite")
            .test(/\.svg$/)
            .include.add(dir).end() // 包含 icons 目录
            .use("svg-sprite-loader").loader("svg-sprite-loader").options({extract: false}).end();
        /* eslint-disable */
        config.plugin("svg-sprite").use(require("svg-sprite-loader/plugin"), [{plainSprite: true}] || []);
        config.module.rule("svg").exclude.add(dir); // 其他 svg loader 排除 icons 目录
    }
};

Pussy answered 9/7, 2020 at 4:31 Comment(2)
That will disable all eslint checks for the entire file. It would be far better to disable it for only the rule and line needed.Metrics
// eslint-disable-next-line @typescript-eslint/no-var-requires is a better optionCalton
A
3

may be you need dynamic module loading, and the code like this:

import {IUser} from './lib/user';
const users:IUser[] = require(path.join(process.cwd() + "/data"));
Adrien answered 30/3, 2017 at 9:28 Comment(0)
D
1

Needed to use provide plugin, yet neither require nor dynamic imports worked for me, this construct helped though:

module.exports = {
  // ..
  chainWebpack: (config) => {
    config.plugin('provide').use(require('webpack').ProvidePlugin, [
      // ..
    ])
  },
Dibbell answered 2/7, 2020 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.