【问题标题】:how can i import package from node_modules when use express node js使用express node js时如何从node_modules导入包
【发布时间】:2020-09-30 22:56:40
【问题描述】:

导入包时出现此错误:

未捕获的类型错误:无法解析模块说明符“axios”。 相对引用必须以“/”、“./”或“../”开头。

当添加 " ./ " , " ../ " 出现此错误

GET http://localhost:8080/js/axios net::ERR_ABORTED 404(未找到)

package.json

{
  "type": "module",
  "name": "fives",
  "version": "1.0.0",
  "description": "game",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./app.js"
  },
  "author": "ayah alrifai",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.0.15",
    "rimraf": "^3.0.2"
  },
  "dependencies": {
    "axios": "^0.19.2",
    "express": "^4.17.1",
    "socket.io": "^2.3.0",
    "socket.io-client": "^2.3.0"
  }
}

app.js

import http from "http";
import express from "express";
import io from "socket.io-client";
import path from 'path';

const app=express();
const port="8080";

app.use('/image', express.static('./public/img'));
app.use('/css', express.static('./public/css'));
app.use('/js', express.static('./js'));

app.get('/wait', (req, res)=> {
    res.sendFile(path.join(path.resolve()+'/html/await.html'));
});

app.get('/play', (req, res)=> {
    res.sendFile(path.join(path.resolve()+'/html/play.html'));
});

app.listen(port);

等待.js

import {getWaitedPlayers,createUser,deleteUser,isSelected,action} from "./fivesApi.js";
//code

fivesApi.js

import axios from "axios";
//code

.
├── html
│   ├── wait.html
│   └── play.html
├── app.js
├── js
│   ├── wait.js
│   ├── fivesApi.js
│   └── play.js
├── node_modules
├── package.json
├── package-lock.json
└── public
    ├── css
    │   └── fives.css
    └── img
        ├── 2.png
        ├── 5.png
        └── ayah.png

【问题讨论】:

    标签: javascript node.js express npm import


    【解决方案1】:

    花了2个小时终于找到解决办法,你要做的第一件事就是

    npm i parcel-bundler -D
    

    然后在package.json中添加如下两行代码

    "scripts": {
    "dev": "parcel index.html",
    "build": "parcel build index.html"
    }
    

    终于 npm run dev.

    如果你仍然有问题,please open this 链接它只是为我节省了很多时间。

    【讨论】:

      猜你喜欢
      • 2019-08-28
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 2018-08-06
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多