无法从Javascript Typescript中的已编译库中导入变量

时间:2018-12-18 11:32:37

标签: javascript typescript d3.js

这是我的package.json文件

{
  "name": "deep-playground-prototype",
  "version": "2016.3.10",
  "description": "",
  "private": true,
  "scripts": {
    "clean": "rimraf dist",
    "start": "npm run serve-watch",

    "prep": "browserify src/csv.ts -p [tsify] | uglifyjs -c > csv.js && copyfiles analytics.js dist && concat node_modules/material-design-lite/material.min.js node_modules/seedrandom/seedrandom.min.js csv.js > dist/lib.js",
    "build-css": "copyfiles fonts.css dist && concat node_modules/material-design-lite/material.min.css styles.css > dist/bundle.css",
    "watch-css": "concat node_modules/material-design-lite/material.min.css styles.css -o dist/bundle.css",
    "build-html": "copyfiles index.html dist",
    "watch-html": "concat index.html -o dist/index.html",
    "watch-js": "watchify src/playground.ts -p [tsify] -v --debug -o dist/bundle.js",
    "build-js": "browserify src/playground.ts -p [tsify] | uglifyjs -c > dist/bundle.js",
    "build": "npm run prep && npm run build-js && npm run build-css && npm run build-html",
    "watch": "npm run prep && concurrently \"npm run watch-js\" \"npm run watch-css\" \"npm run watch-html\"",
    "serve": "http-server -o -c-1 dist/",
    "serve-watch": "concurrently \"http-server -o -c-1 dist/\" \"npm run watch\""
  },
  "devDependencies": {
    "@types/d3": "^3.5.41",
    "concat": "^1.0.3",
    "concurrently": "3.1.0",
    "copyfiles": "1.0.0",
    "http-server": "^0.11.1",
    "rimraf": "2.5.4",
    "tsify": "^4.0.0",
    "typescript": "^2.9",
    "uglify-js": "^2.8.29",
    "watchify": "^3.11.0"
  },
  "dependencies": {
    "@types/jquery": "^3.3.27",
    "csv-parse": "^4.2.0",
    "csvtojson": "^2.0.8",
    "d3": "^3.5.16",
    "https-proxy-agent": "^2.2.1",
    "material-design-lite": "^1.3.0",
    "seedrandom": "^2.4.3",
    "typings": "^2.1.1"
  }
}

csv.ts文件为:

import {csv} from 'd3';
async function delay(ms: number) {
    return new Promise( resolve => setTimeout(resolve, ms) );
}
export let train = [];
(async()=>{console.log('before delay');csv("training.csv",function(error,data){train = data});await delay(1000);console.log('after delay')})();

我首先尝试将数据加载到名为train的变量中。然后,我尝试将其从lib.js文件中拉入程序:

import {train} from 'lib';

但是编译后出现以下错误:

  

TypeScript错误:src / dataset.ts(17,21):错误TS2307:找不到模块“ lib”。

在此过程中我错过了一些事情。

1 个答案:

答案 0 :(得分:0)

除非您更改模块加载逻辑,否则可能需要一个相对的导入路径:

import { train } from './lib'; // Assuming same folder, otherwise adjust path
如果

'lib'是公认的模块,例如安装到node_modules的依赖项。