收藏
回答

NPM构建失败,提示文件不合法?

非常诡异呀,在我的windows主机下没有问题,换到笔记本出问题了

message:发生错误
SyntaxError: parse js file (/Users/locter/Projects/MiniApp/SmartHouseWeApp/node_modules/@npmcli/move-file/lib/index.js) failed: Unexpected token (106:14)
appid: wxada906fdcb4b8b94
openid: o6zAJs7Qulzcdu7d4X8tFuzN3jc0
ideVersion: 1.06.2405020
osType: darwin-arm64
time: 2024-08-02 08:39:11


这是报错提示下的目录,我看也没有使用什么新语法

const { dirname, join, resolve, relative, isAbsolute } = require('path')
const rimraf_ = require('rimraf')
const { promisify } = require('util')
const {
  access: access_,
  accessSync,
  copyFile: copyFile_,
  copyFileSync,
  readdir: readdir_,
  readdirSync,
  rename: rename_,
  renameSync,
  stat: stat_,
  statSync,
  lstat: lstat_,
  lstatSync,
  symlink: symlink_,
  symlinkSync,
  readlink: readlink_,
  readlinkSync,
} = require('fs')


const access = promisify(access_)
const copyFile = promisify(copyFile_)
const readdir = promisify(readdir_)
const rename = promisify(rename_)
const stat = promisify(stat_)
const lstat = promisify(lstat_)
const symlink = promisify(symlink_)
const readlink = promisify(readlink_)
const rimraf = promisify(rimraf_)
const rimrafSync = rimraf_.sync


const mkdirp = require('mkdirp')


const pathExists = async path => {
  try {
    await access(path)
    return true
  } catch (er) {
    return er.code !== 'ENOENT'
  }
}


const pathExistsSync = path => {
  try {
    accessSync(path)
    return true
  } catch (er) {
    return er.code !== 'ENOENT'
  }
}


const moveFile = async (source, destination, options = {}, root = true, symlinks = []) => {
  if (!source || !destination) {
    throw new TypeError('`source` and `destination` file required')
  }


  options = {
    overwrite: true,
    ...options,
  }


  if (!options.overwrite && await pathExists(destination)) {
    throw new Error(`The destination file exists: ${destination}`)
  }


  await mkdirp(dirname(destination))


  try {
    await rename(source, destination)
  } catch (error) {
    if (error.code === 'EXDEV' || error.code === 'EPERM') {
      const sourceStat = await lstat(source)
      if (sourceStat.isDirectory()) {
        const files = await readdir(source)
        await Promise.all(files.map((file) =>
          moveFile(join(source, file), join(destination, file), options, false, symlinks)
        ))
      } else if (sourceStat.isSymbolicLink()) {
        symlinks.push({ source, destination })
      } else {
        await copyFile(source, destination)
      }
    } else {
      throw error
    }
  }


  if (root) {
    await Promise.all(symlinks.map(async ({ source: symSource, destination: symDestination }) => {
      let target = await readlink(symSource)
      // junction symlinks in windows will be absolute paths, so we need to
      // make sure they point to the symlink destination
      if (isAbsolute(target)) {
        target = resolve(symDestination, relative(symSource, target))
      }
      // try to determine what the actual file is so we can create the correct
      // type of symlink in windows
      let targetStat = 'file'
      try {
        targetStat = await stat(resolve(dirname(symSource), target))
        if (targetStat.isDirectory()) {
          targetStat = 'junction'
        }
      } catch {
        // targetStat remains 'file'
      }
      await symlink(
        target,
        symDestination,
        targetStat
      )
    }))
    await rimraf(source)
  }
}


const moveFileSync = (source, destination, options = {}, root = true, symlinks = []) => {
  if (!source || !destination) {
    throw new TypeError('`source` and `destination` file required')
  }


  options = {
    overwrite: true,
    ...options,
  }


  if (!options.overwrite && pathExistsSync(destination)) {
    throw new Error(`The destination file exists: ${destination}`)
  }


  mkdirp.sync(dirname(destination))


  try {
    renameSync(source, destination)
  } catch (error) {
    if (error.code === 'EXDEV' || error.code === 'EPERM') {
      const sourceStat = lstatSync(source)
      if (sourceStat.isDirectory()) {
        const files = readdirSync(source)
        for (const file of files) {
          moveFileSync(join(source, file), join(destination, file), options, false, symlinks)
        }
      } else if (sourceStat.isSymbolicLink()) {
        symlinks.push({ source, destination })
      } else {
        copyFileSync(source, destination)
      }
    } else {
      throw error
    }
  }


  if (root) {
    for (const { source: symSource, destination: symDestination } of symlinks) {
      let target = readlinkSync(symSource)
      // junction symlinks in windows will be absolute paths, so we need to
      // make sure they point to the symlink destination
      if (isAbsolute(target)) {
        target = resolve(symDestination, relative(symSource, target))
      }
      // try to determine what the actual file is so we can create the correct
      // type of symlink in windows
      let targetStat = 'file'
      try {
        targetStat = statSync(resolve(dirname(symSource), target))
        if (targetStat.isDirectory()) {
          targetStat = 'junction'
        }
      } catch {
        // targetStat remains 'file'
      }
      symlinkSync(
        target,
        symDestination,
        targetStat
      )
    }
    rimrafSync(source)
  }
}


module.exports = moveFile
module.exports.sync = moveFileSync



最后一次编辑于  2024-08-02
回答关注问题邀请回答
收藏

2 个回答

  • 启年
    启年
    发表于小程序端
    2024-08-03

    检查你本地路径吧,之前windows换成了mac

    2024-08-03
    有用
    回复 1
    • locter
      locter
      2024-08-03
      是的,是windows换成了mac,但这不应该呀,大佬能否多指点一二?
      2024-08-03
      回复
  • locter
    locter
    2024-08-02

    这个问题已经解决了,是因为 fontmin 这个包导致的,好像不支持mac平台

    2024-08-02
    有用
    回复
登录 后发表内容