Javascript基于运行程序参数动态本地导入标头

Javascript基于运行程序参数动态本地导入标头,javascript,jestjs,Javascript,Jestjs,在Python中,我可以动态地本地导入标题。现在我正在开发通常在客户端上运行的Reactjs和localStorage。现在它已经在服务器端运行了。问题已经得到解决 但是我意外地将mock类提交给源代码。所以我开始使用process.env来进行本地导入就像Python package.json { ..., "scripts": { "start": "react-scripts start", "build": "react-scripts build", "

Python
中,我可以动态地本地导入
标题。现在我正在开发通常在客户端上运行的
Reactjs
localStorage
。现在它已经在服务器端运行了。问题已经得到解决 但是我意外地将
mock
类提交给源代码。所以我开始使用
process.env
来进行
本地导入
就像
Python

package.json

{
  ...,
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}
我知道如何使用
process.env
from。但我仍然无法摆脱这个问题。我得到了
“导入”和“导出”可能只出现在顶层

export const getAuthToken = () => {
  if (process.env === 'development'){
    import localStorage from './localStorageMock';
  }
  return localStorage.getItem('authToken');
};
问题:
如何在
Javascript
中进行本地导入,如
Python
? 如果没有,我如何解决这个问题

尝试1:
localStorageMock2.js
。我尝试另一种语法

function LocalStorageMock(){
  this.store = {};

  this.getItem = function(key){
    return this.store[key] || null
  };
  this.setItem = function(key, value) {
    this.store[key] = value
  };

  this.removeItem = function(key) {
    delete this.store[key]
  };

}
const localStorageMock=new localStorageMock(); 导出默认localStorageMock

Trackback1:
当我查看
缓存
对象时
console.log(缓存)

我想我看到了
removietem
功能。但是没有运气。
console.log(cache.removietem的类型)

然后就像我预料的那样。它引发了一个例外。
返回cache.removietem('authToken')

console.error节点_modules/redux saga/lib/internal/utils.js:240
askBackend TypeError处未捕获:cache.removeItem不是函数
at Object..exports.removeAuthToken(/Users/sarit/study/HT6MInterface/f1/src/utils.js:50:16)
at Object..exports.VerifyTokenReducer(/Users/sarit/study/HT6MInterface/f1/src/containers/reducers.js:20:34)
非常感谢您的光临。他总是通过gitter频道支持新手。这是我问题的完整解决方案

export const myCache = () => {
  //Fix the test when runner is on serverside

  const tmp = Object.assign({}, process.argv);
  const cache = (tmp[4] === '--env=jsdom') ? require("./localStorageMock2") : localStorage;
  if (tmp[4] === '--env=jsdom') {
    const cache = require("./localStorageMock2");
    return cache.default;
  } else {
    return localStorage;
  }
};
我完全忘记了
require
语法。因为我在练习纯ES6

undefined
console.error node_modules/redux-saga/lib/internal/utils.js:240
      uncaught at askBackend TypeError: cache.removeItem is not a function
          at Object.<anonymous>.exports.removeAuthToken (/Users/sarit/study/HT6MInterface/f1/src/utils.js:50:16)
          at Object.<anonymous>.exports.VerifyTokenReducer (/Users/sarit/study/HT6MInterface/f1/src/containers/reducers.js:20:34)
export const myCache = () => {
  //Fix the test when runner is on serverside

  const tmp = Object.assign({}, process.argv);
  const cache = (tmp[4] === '--env=jsdom') ? require("./localStorageMock2") : localStorage;
  if (tmp[4] === '--env=jsdom') {
    const cache = require("./localStorageMock2");
    return cache.default;
  } else {
    return localStorage;
  }
};